For the complete documentation index, see llms.txt. This page is also available as Markdown.

OpenAI Agent SDK

Track usage, control costs, and add guardrails to your OpenAI Agents SDK applications

What is the OpenAI Agents SDK?

The OpenAI Agents SDK is OpenAI's official framework for building agentic AI applications in Python. It provides a lightweight set of primitives—Agents, Tools, Handoffs, and Guardrails—for building multi-step, tool-using agents with minimal boilerplate.

By routing the Agents SDK through FastRouter, you get:

  • 100+ models from OpenAI, Anthropic, Google, xAI, Meta, Groq, Mistral, and more through one endpoint—your agents are no longer locked to OpenAI models

  • Observability for every request: cost, tokens, latency, and model selection tracked in real time

  • Reliability through automatic failover across providers, response caching, and intelligent routing

  • Governance with per-key budgets, rate limits, model restrictions, role-based access, and project isolation

This guide covers connecting the Python Agents SDK to FastRouter, running a tool-calling agent, and model selection. It does not cover the TypeScript SDK.

Prerequisites

  • A FastRouter.ai account (sign up)

  • Python 3.10 or higher


Quick Start

Step 1: Create a Project and Virtual Environment

You'll only need to do this once:

mkdir my_projectcd my_projectpython -m venv .venv

Activate the virtual environment. Do this every time you start a new terminal session.

On macOS or Linux:

On Windows:

Step 2: Install the OpenAI Agents SDK

Step 3: Get Your FastRouter API Key

  1. Sign up or log in at fastrouter.ai

  2. Navigate to your project's Keys page

  3. Click Create User Key

  4. Copy the key immediately. FastRouter does not display the key again after creation.

Export it in your terminal:

Step 4: Point the SDK at FastRouter

The Agents SDK accepts any OpenAI-compatible client. Create an AsyncOpenAI client with FastRouter's base URL and pass it to your agent's model. Save this as agent_example.py:

Note: set_tracing_disabled(True) is recommended because the SDK's built-in tracing exports to OpenAI's platform and requires a native OpenAI key. All FastRouter requests remain fully visible in your FastRouter Dashboard.

Step 5: Run the Agent

The agent calls the get_weather tool and responds:

<img src="../.gitbook/assets/fastrouter-openai-agents-sdk-terminal.png" alt="OpenAI Agents SDK running a tool-calling agent through FastRouter" height="214" width="608">

Every request, token count, and cost now appears in your FastRouter Dashboard.


Use the Agents SDK with 100+ Models

FastRouter uses the provider/model-name format. Switching your agent to a different provider is a one-line change:

You can give each agent in a multi-agent workflow a different model—a fast, inexpensive model for triage agents and a frontier model for reasoning-heavy agents—all through the same client and key.

Explore the full model catalog

Automatic Model Selection

Let FastRouter pick the best model for each request based on query complexity, domain, and cost:

Explore automatic model selection

Flex Pricing for Batch Agent Workloads

Agent pipelines that run unattended—evaluation sweeps, data extraction, report generation—tolerate higher latency. Append :flex to any model to access up to 50% lower token costs:

Note: Flex is not recommended for interactive sessions where you need low-latency responses.

Compare flex pricing options


FAQs

Configuration & Setup

Can I use multiple models with the same API key?

Yes. The API key controls access and budget. The model is selected per agent in code, and you can switch models at any time without changing your key.

Why do I see tracing errors or OPENAI_API_KEY warnings?

The SDK's built-in tracing exports to OpenAI's platform, which requires a native OpenAI key. Call set_tracing_disabled(True) after creating your client. Your request telemetry is still captured in the FastRouter Dashboard.

Can I restrict a key to only use specific models?

Yes. When creating or editing a key, use the Select Models setting to limit which models the key can access. FastRouter rejects requests to unauthorized models.

Tool calls crash with ValidationError: ... arguments — Input should be a valid string. What's wrong?

Some model routes currently return tool-call arguments as a JSON object instead of the JSON-encoded string the OpenAI specification requires, and the Agents SDK validates this field strictly. If you hit this error, switch to a model verified for tool calling through FastRouter, such as openai/gpt-5.2 or anthropic/claude-4.5-sonnet.

Costs & Budgeting

What happens when a key exceeds its budget?

FastRouter blocks further requests until the budget resets (if a reset interval is configured) or an admin increases the limit. The agent receives an error response indicating the budget has been exceeded.

Performance & Reliability

Does FastRouter add latency to agent runs?

FastRouter adds near-zero gateway overhead. For multi-step agent runs, this is negligible compared to model inference time.


Next Steps

Last updated