Chat
OpenRouter Chat API examples: basic agents, image generation, ordered model fallback, retries, structured output, and tool use.
Use OpenRouter's Chat API to reach multiple models through one endpoint. Give an agent an ordered fallback list so a request that hits a rate limit, timeout, or overload retries on the next model.
For details, see examples in cookbook/90_models/openrouter/chat.
| Example | Description |
|---|---|
| Basic | Run a minimal OpenRouter agent with sync, streaming, and async response variants. |
| Dynamic Model Router | Configure an OpenRouter agent with an ordered fallback model list so a request failing on rate limits, timeouts, or overload retries on the next model. |
| Retry | Review retry settings and why invalid model IDs cannot reliably exercise the retry path. |
| Structured Output | Compare JSON mode and native structured outputs on OpenRouter by generating a MovieScript Pydantic model. |
| Tool Use | Give an OpenRouter agent WebSearchTools and stream the answer in both sync and async runs. |
| Image Generation | Generate an image through OpenRouter and display it with Pillow. |