
Subscribe
Tags
Voice AI
AI Agents
Developer Tutorials
Across nine posts I'm building a jack-o'-lantern that sees and talks to trick-or-treaters, and this is part 7. The build starts in part 1. In part 6 the pumpkin got its voice. In this part my server takes over two jobs the model can't do reliably. It counts how long a group has stayed, and it decides which mode the pumpkin should be in.
Why the server is in charge
A model can't count turns reliably. Ask it to end visits after three exchanges, and it'll shoo one group on its first line and chat with the next for ten minutes. The pumpkin's character makes this worse, because underneath the sneering it wants company, and left to itself it would talk forever. I know, I tried it.
Modes have the same problem. Part 2 built three of them as contexts: the yard, the act, and gentle mode. Only a tool result can move the pumpkin between contexts, and a tool runs only when the model calls one. Tell the model in prose to switch to gentle mode and it usually will. Sometimes it ignores the request, and a small child gets the act.
The server sees every exchange as it happens, so I gave it both jobs. It counts turns in code, decides the mode in code, and makes the switch happen by attaching it to the next tool call.
Listen to the call
To count exchanges and know which mode the pumpkin is really in, the server has to watch the call as it happens. Debug events are how it watches. The agent can send my server a stream of debug events covering speech detected, replies generated, tools called, and context and step changes. Level 2 also includes what visitors and the pumpkin said. Turning it on takes one line in the agent's constructor, plus a handler.
An exchange is a visitor speaking and the pumpkin replying, which shows up as a speech_detect event followed by an ai_completion. When the pumpkin mutters to an empty yard, that isn't an exchange, because nobody spoke first.
The same events tell the server where the pumpkin really is. A context_change event says which context the platform switched to, and a session_start means a new call, which always begins in the yard.
Move people along with moods
Part 2's base prompt has a "Right Now" section that reads ${global_data.visit_mood}, and the server changes that value as a visit goes on. During the first exchange, the mood lets the pumpkin ask a question of its own. After that it speaks in statements, because questions keep a conversation going. From the third exchange, which I call the shoo point, each mood pushes a little harder toward the door.
The server sends each new mood into the call with our REST API's calling.ai_message, carrying only global_data. That merges into the call's data silently, without prompting a reply, and the pumpkin's next line reads it. A silent change waits for the model to notice it, though. So at the shoo point the server also says one message into the call, and the pumpkin switches on that turn.
Gentle mode never asks questions, since a talkative child needs no encouragement, and it starts its send-offs an exchange sooner.
Keep a visit state
The server keeps its own record of the group in front, which I call the visit state. It holds the mode the pumpkin should be in and the mode the platform last reported. The server changes the mode it wants when the stage camera reports an arrival or a departure (part 5). It also changes it when the age check fails (part 4) or the operator flips a switch on their phone remote (part 8).
A new group doesn't get the act right away. It waits in the yard, whose greeting suits anyone, until the notice_visitors tool passes the age check. Only then does the server want the act. If the pumpkin is in gentle mode when a new group arrives, it stays gentle until that group passes its own check.
Correct on every tool call
This correction exists because this application has to force a context switch from outside the conversation. The operator's button that you'll see in part 8 and the camera's check for a small child both need to override the mode. The model knows about neither of them. Only a tool result can change contexts, so the server puts the switch on the next tool call. Every tool request passes through on_function_call, which the agent overrides. Before the tool runs, the server compares where the pumpkin is with where it should be. If they differ, the tool's answer belongs to the wrong mode, so the server returns the switch instead.
_switch_to returns a result that changes the context, such as the same enter_gentle result the age check uses. The tools that change modes on purpose, like become_gentle and resume_act, ask the visit state instead. It grants a switch toward gentle mode during a visit and refuses anything else it didn't decide.
The server counts a switch as done only once the platform reports it. It sends the switch on a tool result and waits for the context_change event. Until that event arrives, every tool call sends the switch again.
When no tool call comes
A correction needs a tool call to ride on, and sometimes the model goes a while without calling one. If the pumpkin has been in the wrong mode for 8 seconds, the server sends a message asking for the tool that carries the fix.
After 30 seconds in the wrong mode, the server restarts the call. The pumpkin page waits for the pumpkin to finish speaking, hangs up, and dials again. Every new call starts in the yard, and the page announces whoever is on stage. The pumpkin answers the announcement with a tool call, and that call sets the new call straight. The same restart fires if the pumpkin repeats itself three times in two minutes.
The server allows one restart every 5 minutes. Past that, it raises an alarm instead, which shows up on the operator's phone remote. Ten looks in ten minutes that come back empty raise one too, and so does a visit over ten minutes. Everything the server sends into the call goes through one ordered queue, so a mood change, a nudge, and an arrival can't overtake each other.
Traps to avoid
Don't ask the model to count turns or switch modes in prose. It won't do either reliably. Count in code, and carry the switch on a tool result.
Don't count a switch as done when you send it. Another message can interrupt it, and the switch is lost. Wait for the platform's context_change report, and resend the switch until it arrives.
Don't give a new group the default mode before checking it. Wait in a mode that suits anyone until the check passes.
Next
The server now runs the show, but sometimes a person standing in the yard knows better than any check. In part 8 I build the phone remote that lets them overrule the server.
The series
The agent: a personality and three modes, as contexts and steps.
Tools: what the AI calls on my server, and why its material lives in code.
Vision: letting the agent see, and checking the camera before any decision that matters.
The stage: detecting who is standing in front of the pumpkin, in the browser.
Pacing and state: the server keeps score (this post).
The operator: a person with a phone who can overrule the agent.
Testing it, and running it for a whole night.
Related Resources






