# Build a chat app

> Build a streaming chat app on Agent Router with multi-turn conversations, streaming responses, and automatic failover across providers.

A streaming, multi-turn chat application built on Tetrate Agent Router, with automatic failover across providers.

:::info
All code snippets in this guide are Python. Any OpenAI SDK is supported; Python is not required.
:::

## Architecture

Requests flow from the frontend through the application backend to Agent Router, which routes to a provider and streams the response back over server-sent events (SSE).

```mermaid
flowchart LR
    U[User] --> F[Frontend]
    F -->|POST /api/chat| B[Your Backend]
    B -->|stream: true| T[Agent Router]
    T --> P1[OpenAI]
    T -.->|fallback| P2[Anthropic]
    P1 -->|SSE stream| B
    B -->|SSE stream| F
    style T fill:#ff5500,stroke:#ff5500,color:#fff
```

| Built by the application | Handled by Agent Router |
| --- | --- |
| Frontend UI, conversation state, API route | Provider routing, streaming, failover, cost tracking |

## Step 1: set up the client

A FastAPI application exposes a single `POST /api/chat` route, with an OpenAI client configured against the Agent Router API key and base URL. The route reads the message array from the request body, opens a streaming completion against Agent Router, and relays each token to the caller as a server-sent event. `stream_options={"include_usage": True}` requests token-usage data in the final chunk, and the `text/event-stream` media type on the `StreamingResponse` lets the frontend render the reply as it arrives.

```python title="Python"
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from openai import OpenAI
import os
import json

app = FastAPI()
client = OpenAI(
    api_key=os.environ["AGENTROUTER_API_KEY"],
    base_url="https://api.router.tetrate.ai/v1",
)

@app.post("/api/chat")
async def chat(request: Request):
    body = await request.json()
    messages = body.get("messages", [])

    stream = client.chat.completions.create(
        model="gpt-5.6-terra",
        messages=messages,
        stream=True,
        stream_options={"include_usage": True},
    )

    async def generate():
        for chunk in stream:
            delta = chunk.choices[0].delta if chunk.choices else None
            if delta and delta.content:
                yield f"data: {json.dumps({'content': delta.content})}\n\n"
            # Usage comes in the final chunk
            if hasattr(chunk, 'usage') and chunk.usage:
                yield f"data: {json.dumps({'usage': {'prompt_tokens': chunk.usage.prompt_tokens, 'completion_tokens': chunk.usage.completion_tokens}})}\n\n"
        yield "data: [DONE]\n\n"

    return StreamingResponse(generate(), media_type="text/event-stream")
```

## Step 2: multi-turn conversations

The chat completions API is stateless, so the full conversation must accompany every request. A running `conversation` list holds that history: the system prompt seeds it, each user message is appended before the request, and each assistant reply is appended after it. Every turn therefore sends the complete dialogue, so the model can resolve follow-up questions such as "Can you give me an example?" against everything said earlier. The same array is what the streaming route in Step 1 receives from the frontend.

```python title="Python"
conversation = [
    {"role": "system", "content": "You are a helpful assistant."},
]

def chat_turn(user_message: str) -> str:
    conversation.append({"role": "user", "content": user_message})

    response = client.chat.completions.create(
        model="gpt-5.6-terra",
        messages=conversation,
    )

    assistant_message = response.choices[0].message.content
    conversation.append({"role": "assistant", "content": assistant_message})
    return assistant_message

# Each turn includes full history
print(chat_turn("What's Agent Router?"))
print(chat_turn("How does fallback routing work?"))
print(chat_turn("Can you give me an example?"))
```

## Step 3: add fallback for production

A <Term id="fallback-policy">fallback policy</Term> is an ordered list of backends (a model on a provider) attached to the API key. When the primary backend returns a retryable error, such as a rate-limit response, a server error, or a timeout, Agent Router automatically moves to the next backend in the list, so one outage does not interrupt the conversation. The policy is configured in the Console rather than in code, so the backend from Step 1 stays unchanged: the same `chat.completions.create` call gains failover the moment the policy is in place.

## Step 4: track usage

Because every request passes through Agent Router, token usage and cost are recorded centrally, with no per-application instrumentation. With `stream_options: { include_usage: true }` set as in Step 1, the final SSE event carries the prompt and completion token counts, even for providers that do not include them natively, so the frontend can update a running token or cost figure at the end of each reply.

Per-model cost breakdowns are available in the Console, or through the Usage API for programmatic reporting.

Where to go next

  <Link to="/agent-router-service/quickstarts/make-an-api-call/" className="tare-nav-card">
    Make an AI API call
    The single-request basics: API key, base URL, streaming, embeddings, and images.
  </Link>
  <Link to="/agent-router-service/guides/improve-resilience-with-fallbacks/" className="tare-nav-card">
    Improve resilience with fallbacks
    Formalize the failover policy behind the chat app into an explicit fallback chain.
  </Link>
