Agents from scratch (TypeScript) · Chapter 18 of 18
Interview round
By the end of this chapter you have answered five timed questions, one idea each, from across the whole lab: spotting a repeated call, rebuilding streamed tool calls from the raw wire, validating arguments so a model can correct itself, resuming a paused run, and explaining a trace, all in TypeScript. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 5 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Agents from scratch (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Vikram will not hand the booking agent to anyone who has only read about it. "Short problems, ten minutes each. I am not checking that you remember the openai package. I am checking that you know what the loop does at every step, and that you look at what really went over the wire before you guess." Anjali wants GharFix's next AI engineer interviewed the same way, so the round is now part of the lab.
Each question is one idea from an earlier chapter, with no hand-holding: the file has the goal, the check has the edge cases. The ideas are below, as a refresher and as what an interviewer listens for. The answers are not.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: repeat-guard.ts, a repeat guard
Time yourself: 10 minutes. Export a class RepeatGuard whose seen(name, args) returns true when this run already saw the same call, and otherwise remembers it and returns false. args is the model's JSON text: other spacing, or keys in another order at any depth, is the same call; text that is not JSON is compared as it is. A new RepeatGuard remembers nothing.
Then export async function runAgent(question, model = 'lab-chat'): the loop with a system message saying today is 2026-10-22, the question and GharFix's TOOLS, at most 8 requests. When a reply asks for a call already seen, run nothing from it, send the conversation once more with tool_choice 'none', and return that answer; otherwise return the final answer.
The check tries seen on many spellings (an object inside the arguments too), and runs lab-chat-loopy and a booking with no slot. Careful: JSON.stringify keeps keys in the order they were written, and a Set compares arrays by identity, not by what is in them.
- Task 2 · interview question
Interview: sse-calls.ts, tool calls from the wire
Time yourself: 10 minutes. Export function callsFromSse(lines): lines is an array of the raw lines of a streamed reply (server-sent events, read without the openai package): data lines holding one chunk of JSON each, blank lines, and data: [DONE] at the end. Return the reply's tool calls in the order of their index, each an object { id, name, arguments } with the whole arguments text; a reply with no calls gives an empty array.
The check reads lab-chat's stream, and another provider's that sends comment lines (starting with a colon), writes data: with no space after the colon, mixes two calls together and puts arguments in a call's first piece.
- Task 3 · interview question
Interview: validate-retry.ts, validate, and let it retry
Time yourself: 10 minutes. Export function validatedFind(argumentsJson): validate the model's JSON text for find_technicians with a zod schema (service one of plumbing, electrical, carpentry or ac; area text; date written YYYY-MM-DD and slot morning, afternoon or evening, both optional; no other keys). When valid, run findTechnicians and return its result as JSON text; otherwise run nothing and return JSON text of an object with an error key whose text names every field that failed, an extra key included. Text that is not JSON gets an error too, never a throw.
Then export async function run(question): the loop with lab-chat-confused, a system message saying today is 2026-10-22, the question and GharFix's TOOLS, at most 6 requests, every find_technicians call run through validatedFind (other calls as they are, an error result when one cannot run). Return the final answer.
The check sends wrong arguments of five kinds, then a booking the confused model gets wrong on its first call.
- Task 4 · interview question
Interview: resume.ts, resume a paused run
Time yourself: 10 minutes. A run paused before a dangerous call is saved as JSON: status waiting, messages (the conversation, ending with the assistant's message that asked for the call), pending (each call's id, name and arguments) and answer null. Export async function resume(path, approved): for a waiting run, give each pending call its tool message (approved: run it with GharFix's function and send the result; not approved: run nothing and send an error result), empty pending, and go on with lab-chat and GharFix's TOOLS, at most 6 more requests, until the model answers. Save the run to the same file with status done and the answer, and return the answer. For a run that is not waiting, return its answer and ask the model nothing.
The check pauses a booking, resumes it once with yes and once with no, and then resumes each again.
- Task 5 · interview question
Interview: explain-trace.ts, explain a trace
Time yourself: 10 minutes. Export function diagnose(path, traceId): read that trace's spans from a trace file (one JSON span per line: traceId, span, kind, step; finishReason on a model span; tool, arguments, resultChars and error on a tool span, as chapter 12 writes them) in the order of their span number. Going through the tool spans in order, return the first of: error at step <step>, <tool>: <error> for a span with an error; empty result at step <step>, <tool> for a result of 2 characters; repeat at step <step>, <tool> for the same tool with the same arguments as an earlier span (as JSON, in any spelling). If none, return no answer: stopped after <n> requests when the last model span did not finish with stop, and ok otherwise.
The check writes five traces into one file, one of them with its spans out of order, and a clean one.
What you will learn
- How to work a timed question: say the plan, build the smallest thing that runs, then measure it against the real API.
- Five ideas again, at interview depth: repeats (chapter 6), streamed calls (13), validation (7), paused runs (9 and 10) and traces (12).
- Where TypeScript itself trips you up in each: key order in JSON.stringify, a Set of arrays, JSON.parse before zod, an async callback nobody waits for.
More in Production
Build "Interview round" yourself
GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. You build it in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Agents from scratch (TypeScript) are free. This one comes with the all labs plan.