When a business connects a conversational language model directly to a live customer phone line without rigid structural boundaries, catastrophic failure is only a matter of time. Telling an AI in a prompt to “always quote our standard rates and never promise unauthorized discounts” is not engineering—it is wishful thinking. In voice telephony, production reliability requires separating conversational fluency from deterministic action execution. Here is how we engineered structured tool calling, fail-closed dispatch, and ephemeral session isolation at Ghost AI Systems.
In the demo economy, voice AI looks deceptively simple. You open a WebRTC connection, pipe incoming microphone audio to a multimodal speech-to-speech model, and listen as the agent answers questions with fluid, human-like cadence.
In a production commercial setting, that simplicity evaporates. A customer calling a luxury realtor, a regional medical practice, or a high-ticket contractor is not looking for a creative writing exercise. They are asking about real pricing, scheduling consultations, requesting services, and providing personal contact details. If the voice agent improvises a non-existent $200 discount, promises an appointment time that conflicts with the owner’s calendar, or hallucinates an unverified service guarantee, the business pays the bill in lost revenue and compromised reputation.
The fundamental flaw of open-ended conversational bots is granting the model direct agency over business logic. Large language models excel at natural language synthesis, sentiment recognition, and fluid phrasing. They are notoriously poor at rigid arithmetic, state tracking, and policy enforcement when left unconstrained.
To deploy voice agents that businesses can safely trust on their primary phone numbers, we designed an architecture built on five deterministic guardrails:
- Schema-Constrained Tool Registries: Enforcing strict JSON schemas with closed property validation and fail-closed dispatch handlers.
- Ephemeral Session Lifecycle & Rate Quotas: Isolating root API credentials and throttling session initialization to block denial-of-wallet exploits.
- Half-Duplex Acoustic Gating: Dropping microphone audio frames during agent speech playback to eliminate acoustic self-interruption loops.
- Stream Isolation & Loop Circuit Breakers: Filtering internal function executions out of user-facing transcripts and capping multi-turn execution loops.
- Deterministic Quoting & Validation Engines: Moving all calculations and qualification rules out of model prompts into immutable TypeScript logic.
1. Schema-Constrained Tool Registries and Fail-Closed Dispatch
When our voice agents need to calculate a quote, check availability, or route a lead, they do not invent the logic in prose. They trigger a structured function call defined in lib/tools.ts.
Every tool available to the agent—whether quoting service packages (quote_service), qualifying prospects (qualify_lead), or scheduling appointments (check_availability)—is registered under strict JSON Schema parameters:
- Disallowed Undeclared Properties: Every tool schema explicitly enforces
additionalProperties: false. The model cannot inject extraneous fields, undocumented flags, or unexpected payloads into backend handlers. - Mandatory Parameter Validation: Required arguments (such as package names or prospect contact details) are validated before any internal handler executes.
- Fail-Closed Tool Dispatch: The central dispatcher (
runTool) uses an explicit, exhaustive switch statement across recognized tools. If an agent attempts to execute an unmapped, hallucinated, or obsolete function name, the system does not guess or fall back—it throws an immediate error and aborts execution.
2. Ephemeral Session Tokens & Denial-of-Wallet Defenses
Deploying voice AI directly to web browsers or client devices presents a severe authentication risk. Real-time audio protocols require low-latency WebRTC channels. If client-side code directly accessed master OpenAI or xAI API keys, anyone with browser developer tools could extract the key and run thousands of billable audio hours on the agency’s tab.
We architected our session initialization routes (/api/voice/xai-session and /api/voice/session) with strict credential isolation:
- Master Keys Remain Server-Side: Client browsers never receive long-lived API secrets. Instead, the server uses master credentials to mint an ephemeral, single-use client session token directly from the model provider.
- Strict Time-to-Live (TTL): Each minted session token carries an immutable lifetime window capped at 300 seconds (5 minutes). Once expired, the connection terminates automatically.
- Trusted Client-IP Rate Limiting: Session generation is gated by our trusted IP rate limiter (resolving through Cloudflare and Vercel headers), capping session minting at 5 to 8 requests per hour per IP.
This combination prevents denial-of-wallet attacks and protects upstream API quotas, ensuring public demonstrations and client trials remain economically bounded.
3. Half-Duplex Acoustic Gating for Clean Telephony
One of the most persistent issues in live voice AI is acoustic echo and self-interruption. When the voice agent speaks through a phone earpiece or device loudspeaker, the sound wave travels through physical space and re-enters the microphone.
Neural Voice Activity Detection (VAD) models monitor audio inputs to allow callers to interrupt the agent naturally. However, if the microphone captures the tail end of the agent’s own speech, the VAD algorithm often interprets the echo as a human voice trying to speak. The agent abruptly cuts itself off mid-sentence, stutters, and loses conversational context.
In lib/use-xai-voice-agent.ts, we addressed this challenge at the WebRTC transport layer:
Callers can still deliberately speak over the agent when needed, but the agent will never cut itself off due to acoustic reflections from its own voice.
4. UI Stream Isolation & Multi-Turn Circuit Breakers
In a typical voice agent turn, the model may execute several background tools before formulating its spoken reply. For instance, when a caller asks, “Can you quote your starter plan and see if you have time Thursday?”, the model triggers a pricing calculation followed by an availability lookup.
If these raw function calls and JSON responses were piped directly to the user interface or speech synthesizer, the caller would hear an unintelligible stream of function names and curly braces.
Our real-time event pipeline maintains strict separation between conversational and operational turns:
- Presentation Filtering: In
components/home/VoiceHero.tsx, tool execution turns are filtered out of public message arrays (messages.filter(m => m.role !== 'tool')). Callers experience an uninterrupted conversational flow while business operations complete invisibly in background state. - Execution Loop Circuit Breakers: In autonomous agent loops, a model can occasionally get stuck oscillating between multiple tools. Both our concierge and voice dispatch handlers enforce a strict circuit breaker of
maxRounds = 5. If an execution chain fails to resolve into a final answer within five turns, the system terminates the loop cleanly rather than racking up runaway inference costs. - Input Sanitization: Server-side prompt construction explicitly filters out any inbound message payload claiming to hold a
toolrole. An external caller cannot spoof the outcome of a database verification or booking confirmation by injecting fake tool responses into the conversation stream.
5. Deterministic Quoting Over Conversational Arithmetic
Perhaps the highest risk for any commercial voice agent is quoting prices. If an agent is told in its prompt that “pricing starts at $497/mo, but offer flexibility for larger teams,” the model will eventually offer arbitrary numbers to assertive callers.
In our architecture, pricing calculations are strictly deterministic:
- AI Voice Agent Tiers: Starter is hard-coded at $497/mo + $497 setup (200 minutes), Pro at $997/mo + $497 setup (500 minutes), and Enterprise at $1,997/mo + $497 setup (unlimited minutes).
- SiteDrop Web Builds: Micro tier is hard-coded at $1,000, Standard at $5,000, and Custom at $8,500.
- Immutable Calculation Engine: When the agent calls
quote_service, the pricing function inlib/tools.tscalculates totals by multiplying vetted base prices and verified add-on quantities. The language model only receives the precomputed total; it never computes the math itself.
If a caller asks for an unapproved discount, the agent can politely decline because the underlying tool physically lacks an argument for arbitrary price overrides.
Production Voice Agents Built for Real Revenue
There is a vast gulf between a viral voice bot demo and a reliable voice agent that handles inbound calls for an active business. Free-form chat is fine for entertainment; commercial telephony requires deterministic boundaries, cryptographic authentication, and fail-closed state machines.
By anchoring our voice architecture in schema-validated tools, ephemeral tokens, acoustic echo gating, and deterministic business logic, we deliver voice agents that answer 24/7 without risking client reputation or financial leaks.
Test our real-time voice interface live on our homepage demo, evaluate your potential missed-call recovery with our Voice AI Scorecard, or explore full deployment packages on our AI Voice Agent platform.