Skip to main content
OpenRouter gives you one API key and one billing relationship for hundreds of models from every major provider (Anthropic, OpenAI, Google, Meta, DeepSeek, Z.AI, Tencent, and more). Because every model shares the same interface, it’s the ideal backend for mixing providers inside a single swarm — a Claude agent debating a GLM agent debating a Llama agent, all in one room. This tutorial builds three things end-to-end:
  1. A single agent running on an OpenRouter model.
  2. A group chat where agents on different OpenRouter models discuss a topic.
  3. A concurrent workflow that fans the same task out to several OpenRouter models at once.

Installation

Environment Setup

Get a key at openrouter.ai/keys.

Model Naming

OpenRouter model names follow the pattern openrouter/<provider>/<model>: Browse the full catalog for hundreds more. Any model tagged :free costs nothing to call — great for prototyping.

1. Single Agent

The smallest useful program: one Agent on one OpenRouter model. Note the OpenRouter-friendly defaults — set top_p=None so temperature is sent alone (some providers reject both), and use reasoning_effort/thinking_tokens on models that support extended reasoning.
Swapping the model is a one-line change — point model_name at any entry from the table above (e.g. "openrouter/anthropic/claude-opus-4-8" or "openrouter/tencent/hy3:free").

Streaming

Add streaming_on=True to print tokens as they arrive:

2. Group Chat Across Providers

GroupChat runs a turn-based, self-selecting room: every agent silently bids on how much it wants to speak, and the single highest bidder above threshold takes the floor each turn. Because OpenRouter exposes every provider through one key, you can seat agents backed by different model families at the same table — here Claude, GLM, and Llama debate together. Each agent uses max_loops=1 and persistent_memory=False so every speaking decision is a clean single-shot call. The respond tool each agent needs to bid is injected automatically (auto_equip=True by default).
To iterate over individual messages instead of a formatted string, set output_type="list":
Each message dict carries role (the agent name, or "User" for the seed task) and content. Raise threshold for a more selective room; lower it for a livelier one. See the Group Chat Example for more patterns.

3. Concurrent Workflow (A/B Testing Models)

ConcurrentWorkflow runs every agent in parallel on the same task and returns all responses together. Since OpenRouter puts every model behind one key, this is the cleanest way to A/B test model quality, latency, and style side by side.
Each agent hits a different model concurrently; results maps agent name → response so you can compare answers directly. Add or remove models by editing the candidates list — no other code changes needed.

Production Defaults

For anything beyond a quick script, pin sensible defaults:

Next Steps