Agent SDK
Build AI agents with multi-turn loops, tools, and conversation state
The Agent SDK (@openrouter/agent) provides the primitives you need to build agentic applications on OpenRouter. Instead of manually wiring up conversation loops, tool dispatch, and state tracking, the Agent SDK handles all of that so you can focus on defining what your agent does.
The Agent SDK is built to work alongside the Client SDKs. Installing @openrouter/agent automatically includes the Client SDKs as well, but each package can work independently.
When to use the Agent SDK
Choose the Agent SDK when you need agentic behavior — multi-step reasoning where the model calls tools, processes results, and decides what to do next:
- Multi-turn agent loops —
callModelautomatically loops until a stop condition is met - Tool definitions — define tools with the
tool()helper and the SDK executes them for you - Stop conditions — control when the loop ends with
stepCountIs,hasToolCall,maxCost, and more - Conversation state — the SDK tracks messages, tool results, and context across turns
- Streaming — real-time token output within each agent step
- Dynamic parameters — change model, temperature, or tools between turns based on context
If you only need simple request/response calls to a model without agent loops, the Client SDKs are a lighter-weight option.
Installation
Quick example
The SDK sends the message to the model, receives a tool call, executes get_weather, feeds the result back, and returns the final response — all in one callModel invocation.
Core concepts
callModel
The main entry point. It runs an inference loop that:
- Sends messages to the model
- If the model returns tool calls, executes them automatically
- Appends tool results to the conversation
- Repeats until a stop condition is met or no more tool calls are made
See the Call Model documentation for the full API.
Tools
Define tools with the tool() helper. Each tool has a name, description, Zod parameter schema, and an execute function. The SDK handles serialization, validation, and dispatch.
Stop conditions
Control when the agent loop terminates:
Agent SDK vs Client SDKs
Next steps
- Call Model — the complete
callModelAPI reference - Tools — defining and using tools
- Stop Conditions — controlling agent loop termination
- Streaming — real-time token output
- DevTools — telemetry capture and visualization for development
- Migrating from @openrouter/sdk — move agent imports to the standalone package