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.

ExampleDescription
BasicRun a minimal OpenRouter agent with sync, streaming, and async response variants.
Dynamic Model RouterConfigure an OpenRouter agent with an ordered fallback model list so a request failing on rate limits, timeouts, or overload retries on the next model.
RetryReview retry settings and why invalid model IDs cannot reliably exercise the retry path.
Structured OutputCompare JSON mode and native structured outputs on OpenRouter by generating a MovieScript Pydantic model.
Tool UseGive an OpenRouter agent WebSearchTools and stream the answer in both sync and async runs.
Image GenerationGenerate an image through OpenRouter and display it with Pillow.