Agents from scratch (TypeScript) · Chapter 14 of 18

Concurrent tools

By the end of this chapter you can serve many customers from one program, run a reply's independent tool calls at the same time with Promise.all and Promise.allSettled, and put a time limit on every tool and every model request with AbortSignal and the SDK's own options. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 4 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

GharFix's bot is one Node program answering every customer on WhatsApp. At 9 in the morning that is dozens of conversations at once, and each booking is several requests to the model, a second or so each. A program that finishes one customer's run before it starts the next makes everyone wait in a single queue.

Inside one run there is waiting too. Vikram's team moved the technicians' calendar to a service of its own, and a check_slots call now takes about a second over the network. A booking with no slot asks for it once per technician, three calls in one reply, and they wait for each other for no reason. Then one morning the calendar service hangs, and every customer who asks for a slot waits with it, forever. Meenakshi gets the complaints.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    two-customers.ts: two customers at once

    GharFix's server answers many customers at once. In two-customers.ts, write and export runAgent(question), async: the loop with lab-chat, a system message saying today is 2026-10-22, the question and GharFix's TOOLS, at most 10 requests, each call run with FUNCTIONS; return the final answer's text.

    When the file is run (guard it with if (import.meta.main)), serve Meenakshi's two requests at the same time, exactly:

    Book a plumber in Domlur 28 Oct morning: the kitchen tap is broken. I am Arjun Menon, phone 9886012345.

    Please move my booking GF318075 to 2026-10-31 evening.

    and print the two answers, the booking's first.

    The check runs the two one after the other, then both at once, and reads every request: each must hold one customer's conversation only.

  2. Task 2

    at-once.ts: a reply's calls at the same time

    GharFix's calendar is now a service of its own. In at-once.ts, export slowCheckSlots(args), async: wait one second (setTimeout from node:timers/promises), then return checkSlots(args).

    Export runCalls(toolCalls), async: run every call of one reply at the same time (check_slots with slowCheckSlots, every other tool with FUNCTIONS) and return one tool message per call ({ role: 'tool', tool_call_id: the call's id, content: the result as JSON text }), in the calls' order. A call whose tool throws (a name that is not in FUNCTIONS) gets the JSON of an object with an error key naming the tool, and every other call keeps its result.

    Then export runAgent(question): the loop with lab-chat, a system message saying today is 2026-10-22, the question and TOOLS, at most 10 requests, using runCalls; after each step's calls, print step N: K calls in S.SS s, and return the final answer.

    The check times runCalls on three check_slots calls and on a mix with a tool that does not exist, and runs a booking with no slot.

  3. Task 3

    timeouts.ts: a time limit for every tool, and for the model

    In timeouts.ts, export FUNCTIONS: GharFix's eight tools as async functions that take the arguments and an AbortSignal. Export SORRY, exactly:

    Sorry, our booking system is slow right now. Please try again in a few minutes.

    Export runTool(name, argumentsJson, ms = 2000), async: run FUNCTIONS[name] with the parsed arguments and a signal that aborts after ms milliseconds, and return the result as JSON text. When the tool has not answered in time, stop waiting, even if the tool ignores the signal, and return JSON text of an object with an error key whose text names the tool.

    Then export runAgent(question, model = 'lab-chat'): the loop with that model, a system message saying today is 2026-10-22, the question and TOOLS, at most 10 requests, every call run with runTool. Give each model request at most 3 seconds and no retries; when one takes longer, return SORRY. Otherwise return the final answer.

    The check swaps in a get_booking that never answers (one listens to the signal, one does not), and asks lab-chat-slow, which takes 4 seconds.

  4. Task 4 · interview question

    Interview: fix-serial.ts, calls that wait in line

    In fix-serial.ts, a booking with no slot fans out into three check_slots calls in one reply, and the loop is meant to run them at the same time. The step still takes about three seconds. Fix it so that every call of one reply runs at the same time: three check_slots calls in about 1 second.

    Keep slowCheckSlots exactly as it is: it stands for the calendar service. Keep runCalls(toolCalls), which returns one tool message per call in the calls' order, runAgent(question) and the line it prints after each step.

    The check times runCalls on three check_slots calls and runs a booking with no slot.

What you will learn

  • Runs at the same time: two customers in one program, each with a conversation of its own.
  • A reply's calls at once with Promise.all, results kept in order, and Promise.allSettled so one failing tool loses nothing else.
  • The interview bug: await inside a for ... of loop, which runs calls one after another.
  • Time limits: AbortSignal.timeout() for a tool, and the SDK's timeout, maxRetries and signal for the model.

More in Production

Build "Concurrent tools" 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.