Build a voice agent · 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
- Python
- In the all labs plan
The first 2 chapters of Build a voice agent 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 (1160 ms after the caller)
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.py: Kickstart's bookings, as tools
Write 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 async def run_tool(name: str, args_text: str) -> str. It parses args_text (the model's arguments, a JSON string), calls Kickstart's bookings API at VOICE with httpx.AsyncClient (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 raises: arguments that are not JSON, or a name it does not know, give JSON text with an "error".
- Task 2
run_tools.py: tools without dead air
Write async def run_tools(calls: list[dict], filler, slow: float = 0.6) -> list[str]. calls are {"id", "name", "arguments"}. It starts every call at once with run_tool (asyncio.gather); if they are not all done after slow seconds it calls filler() once (a timer: loop.call_later, cancelled when the results are in) and does not wait for the filler to be heard; when all are done it returns the results in the calls' order. Calls done within slow get no filler.
The given agent.py 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.py, what a slow tool sounds like
Take two calls from Anil at the same time (asyncio.gather): run_call("anil-reschedule", filler=False) and run_call("anil-reschedule") (from agent). For each, read its record with await call_record(run.call_id) 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 an exception in the middle of a call.
- Parallel tool calls with asyncio.gather: 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, timed with loop.call_later.
- 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 are free. This one comes with the all labs plan.