Build a voice agent (TypeScript) · Chapter 13 of 21
Tools during a call
By the end of this chapter Bolo can look up, move and cancel Kickstart's bookings in the middle of a call, run the lookups it needs at the same time, and never leave the caller in silence while a slow system answers. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real time
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Build a voice agent (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Most of Kickstart's calls are not questions about prices. They are "move my service to Friday", "is my Activa ready", "cancel tomorrow's pickup". Tejas runs the bookings system, and it has an API: look up a booking, find free slots, reschedule, cancel, log a complaint. His condition from chapter 1 still stands: "move the right booking, or none."
Bolo has no way to touch a booking yet. Here is Anil, who wants tomorrow's service moved to the day after:
5.72- 10.14 caller namaste, mera booking number k s chaar chaar do teen hai. kal ki service parson kar dij...
11.32- 14.82 Bolo maaf kijiye main abhi booking check nahi kar sakti hamari team aapko call karegi (1180 ms after the caller)
15.28- 16.40 caller das baje wala theek hai.
17.56- 21.06 Bolo maaf kijiye main abhi booking check nahi kar sakti hamari team aapko call karegi
outcome: caller_hung_up, not a success
Fast, polite, and useless: Kavita's desk gets a callback to make. Giving the model tools fixes that, the way the agents labs showed. The new problem is time. Tejas's API is slow at peak hours: a lookup takes 1.2 seconds, the free slots 0.7, a reschedule 0.9. In a chat window nobody notices. On a phone line, every one of those milliseconds is silence the caller sits through, and after three seconds of it people start saying "hello?".
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
tools.ts: Kickstart's bookings, as tools
Write and export TOOLS, five tools in the chat completions format ({ type: 'function', function: { name, description, parameters: { type: 'object', properties, required } } }): lookup_booking (booking_id), find_slots (date, YYYY-MM-DD), reschedule_booking (booking_id, date, time as HH:MM), cancel_booking (booking_id) and log_complaint (summary; booking_id optional). The names in brackets are each tool's required parameters.
Write and export runTool(name: string, argsText: string): Promise<string>. It parses argsText (the model's arguments, a JSON string), calls Kickstart's bookings API at VOICE (GET /api/bookings/ID, GET /api/slots?date=DATE, POST /api/bookings/ID/reschedule with { date, time }, POST /api/bookings/ID/cancel with {}, POST /api/complaints with { summary, booking_id }) and returns the API's JSON text, refusals too. It never throws: arguments that are not JSON, or a name it does not know, give JSON text with an "error".
- Task 2
run-tools.ts: tools without dead air
Write and export runTools(calls: { id, name, arguments }[], filler: () => Promise<void>, slowMs = 600): Promise<string[]>. It starts every call at once with runTool; if they are not all done after slowMs it calls filler() once and does not wait for it to finish; when all are done it returns the results in the calls' order. Calls done within slowMs get no filler.
The given agent.ts runs the model's tool calls with it. Anil's call (anil-reschedule) must succeed, with the filler heard on both slow turns and his wait before Bolo's first word at most 2,500 ms on each.
- Task 3 · interview question
Interview: dead-air.ts, what a slow tool sounds like
Take two calls from Anil at the same time: runCall('anil-reschedule', { filler: false }) and runCall('anil-reschedule') (from ./agent.ts). For each, read its record with callRecord and print, first the one without: without the filler: A / B / C ms, success BOOL, then with the filler: A / B / C ms, success BOOL (latencyMs of each of the caller's lines that has one, in order, joined with " / "). Last, print longest wait: X ms -> Y ms (the largest wait without the filler, then with it).
What you will learn
- Kickstart's bookings as tools: five definitions, and one function that runs any of them.
- A tool's failure is a result, never a crash in the middle of a call.
- Parallel tool calls: the lookup and the slots at the same time.
- The filler phrase: "Ek second, main check kar rahi hoon", only when a tool is slow.
- Measuring dead air: the caller's wait on the turns that use tools, with and without the filler.
More in Real time
Build "Tools during a call" yourself
Kickstart services scooters at the door in Nagpur, and its phone line gets 600 calls a day in Hindi, English and Hinglish. You build Bolo, its voice agent, and measure every call: who it understood, how long callers waited and who it cut off. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Build a voice agent (TypeScript) are free. This one comes with the all labs plan.