Voice AI

Building an AI-Powered Call Router on the Telnyx Edge Runtime

Routing inbound calls based on what the caller actually wants sounds simple—until you try to build it. Traditional PBX trees ("Press 1 for sales, 2 for support") are rigid and frustrating. What if you could just ask the caller, "How can I help you today?" and let an AI model route them to the right queue?

Today, we're looking at the ai-powered-call-router sample app. It uses the Telnyx Edge Runtime to analyze caller intent with AI and route calls dynamically. Because Telnyx is AI Communications Infrastructure, we can compose Call Control, AI Inference, and Key-Value storage into a single edge function.

What the App Does

When a call comes in, the app answers and greets the caller. It then uses Call Control's AI Gather feature to capture the caller's spoken request. That speech is passed to a Telnyx-hosted LLM for intent classification. The model determines if the caller needs billing, sales, or support. Finally, the app looks up the correct destination number in Telnyx KV and transfers the call.

The entire flow runs on the Edge Runtime. There are no external servers to manage, and no extra API credentials to secure.

How It Works

The architecture relies on a RouterAgent, which is a StatefulActor. The Edge Runtime spins up one actor per call leg. This gives us a durable, isolated execution environment for the lifecycle of the call.

Here is the flow:

  1. Inbound Webhook: A call hits your Telnyx number, triggering a webhook to your edge function.
  2. Answer: The actor answers the call.
  3. Greeting: The app uses Call Control TTS to speak a greeting.
  4. Gather: It calls gather_using_ai to listen to the caller's request.
  5. Classify: The transcript is sent to the AI Inference binding.
  6. Route Lookup: The classified intent is used to query Telnyx KV.
  7. Transfer: The app speaks a final announcement and bridges the call.

The magic happens in the classifyAndRoute function. We use the zero-credential this.env.TELNYX.ai.openai.chat.createCompletion() binding. Because the runtime is tightly integrated with our AI layer, you don't need to manage API keys for the LLM.

// Inside the RouterAgent
async classifyAndRoute(speech: string) {
  // 1. Classify intent using AI Inference binding
  const completion = await this.env.TELNYX.ai.openai.chat.createCompletion({
    model: this.env.AI_MODEL,
    messages: [
      { role: 'system', content: 'Classify intent: billing, sales, or support.' },
      { role: 'user', content: speech }
    ]
  });
  
  const intent = completion.choices[0].message.content.trim().toLowerCase();
  
  // 2. Look up destination in KV
  const destination = await this.env.ROUTES.get(`route:${intent}`) || this.env.DEFAULT_DESTINATION;
  
  // 3. Transfer the call
  await this.telnyx.calls.transfer(this.callControlId, { to: destination });
}

How Transfers Work

The transfer() action is a blind bridge. Telnyx dials the destination from your KV route table. When the destination answers, the two legs connect. The caller hears the spoken announcement on the original leg, then the bridge connects. The transferred leg does not receive a greeting—it is simply bridged.

Setup

You need a Telnyx account and the Telnyx CLI.

1. Create the KV Namespace

First, create the KV namespace for your route table:

telnyx-edge storage kv create --name ai-call-router-routes

Copy the returned ID into your telnyx.toml and .env files.

2. Seed the Route Table

Add your destination numbers to KV:

curl -X PUT "https://api.telnyx.com/v2/storage/kvs/$KV_NAMESPACE_ID/keys/route:billing" \
  -H "Authorization: Bearer $TELNYX_API_KEY" \
  -H "Content-Type: text/plain" \
  -d "+1XXXXXXXXXX"

3. Configure Secrets

Set your API key as a secret:

telnyx-edge secret set TELNYX_API_KEY

4. Deploy and Map

Deploy your edge function and map your Call Control Application's webhook URL to the function endpoint. Update your phone number to point to that Call Control Application.

Conclusion

The ai-powered-call-router demonstrates how AI Communications Infrastructure simplifies complex voice workflows. By combining Stateful Actors, zero-credential AI Inference, and global KV storage on the Edge Runtime, you can build responsive, intelligent telephony applications without managing external services.

Check out the full code on GitHub and deploy your own AI call router today.

Ready to build with low-latency voice AI?

Join developers building the future of real-time conversations