Memory and multi-agent systems in TypeScript · Chapter 13 of 16

Parallel agents

By the end of this chapter you can ask several specialists at the same time with promises and Promise.all, give each one a time limit with an AbortSignal, keep the answers that came when one specialist fails, merge the answers into one reply a family can read, and stay under a provider's rate limit with a gate you write yourself and polite retries. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Many agents
  • 5 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Memory and multi-agent systems in TypeScript are free. This one comes with the all labs plan.

Why you need this

Some questions belong to two specialists. Twelve of the 132 client requests in requests.jsonl say so in their needs: "Can we afford Amer Bagh Palace if we cut the decor?" needs the venue and the budget, "Is the haveli's kitchen big enough for the caterer?" the venue and catering. Policy 6.3 is short: a request that needs two specialists gets both answers, merged into one reply.

Sameer timed the first try. It asked one specialist, waited, then asked the other, and a family waited for both. Karan put it simply: the two specialists do not need each other, so they should not wait for each other. And Rekha added the part nobody had thought of: "The family gets one message. Not two messages, and certainly not the same sentence twice."

Everything you have written so far awaits one request, then the next. That is right when the second request needs the first one's reply (a tool loop, a handoff). It is wrong here: two specialists that read different notes can be asked together, and the code you know has no way to say so.

What you build: 5 tasks

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

  1. Task 1

    parallel.ts: specialists at once

    Write and export ask(name, text, weddingId, model = 'lab-chat', signal?) in parallel.ts: one specialist's answer at temperature 0, sending the specialist's system(weddingId) from specialists.ts as the system message and the text as the user message, with signal passed to the request. Write and export askAll(text, weddingId, names, model = 'lab-chat', timeout = 10): ask every named specialist at the same time and return an object, name to answer, in the order of names; a specialist that has not answered after timeout seconds gives null.

    Inside if (import.meta.main), ask venue and decor about WD1063, Can the photographer shoot the haldi at the venue?, on lab-chat-slow, first one after the other, then with askAll, and print two lines: one after the other: then the seconds with one decimal and s, and at once: the same way. The check times askAll on lab-chat-slow with three specialists, and with a timeout of 1 second.

  2. Task 2

    needs.ts: the requests that need two

    For each request of requests.jsonl whose needs names two specialists, in the file's order, ask both at once with askAll, and print a line with the request's id, a space, its needs joined with a comma and a space, a colon, a space and its text; then for each specialist in the order of needs, two spaces, its name, a colon, a space and its answer.

    End with the line the same answer from both: then how many of those requests got the very same answer from both specialists, of, and how many there were.

  3. Task 3

    merge.ts: one reply from several answers

    Write and export merge(answers, needs) in merge.ts: answers maps a specialist to its answer or null; needs is the order. Return one reply with one line per different answer, in the order of needs: the names that gave it joined with and and a space on each side, the first letter a capital, a colon, a space and the answer. Answers equal after trimming spaces at the ends are the same answer. A null or empty answer gets no line; when there are any, the last line is No answer yet from: and those names joined with a comma and a space, then a full stop.

    Inside if (import.meta.main), for R031 and R055, ask both specialists with askAll and print the request's id, a colon, a space and its text, then merge's reply.

  4. Task 4

    limits.ts: forty questions, three at a time

    Make the OpenAI client in limits.ts with maxRetries: 0. Write and export askMany(questions, limit = 3, model = 'lab-chat-limited'): each question is an array of a specialist, a text and a wedding id; ask each like task 1's ask, never more than limit at a time, and return the answers in the order of the questions. A 429 is retried after the seconds in its retry-after header, up to 4 retries.

    Inside if (import.meta.main), ask the first 40 requests of requests.jsonl, each to its route specialist, and print the number of answers, answers in, the seconds with one decimal, s, a comma, a space, the number of 429s your requests met and refused on the way. The check runs askMany with a limit of 6 too, which lab-chat-limited refuses.

  5. Task 5 · interview question

    Interview: fix-me.ts, parallel agents that wait in line

    askAll(text, weddingId, names) in fix-me.ts should ask every named specialist at the same time. Run it: it asks them one after the other, and a name that is not a specialist loses every answer.

    Fix askAll so that the specialists are asked at the same time and a specialist that fails answers null, while the others' answers are kept, in an object in the order of names. Keep the function's name, its parameters and the export. The check times it on lab-chat-slow and gives it a specialist that does not exist.

What you will learn

  • Requests in flight: a promise, Promise.all, and why an await inside a for loop goes one at a time.
  • A time limit per agent with AbortSignal.timeout(), and why the SDK's own timeout option takes three times as long.
  • One failure is one failure: Promise.allSettled keeps the answers that came.
  • Merging answers into one reply, without repeating anything.
  • Rate limits: the 429 status, retry-after, the SDK's own retries, and a gate that lets three requests through at a time.

More in Many agents

Build "Parallel agents" yourself

Baraat Bliss, a Jaipur wedding planner, must remember months of family decisions across many agents. You build its assistant 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 Memory and multi-agent systems in TypeScript are free. This one comes with the all labs plan.