Openai Agents Python
Openai Agents Python is the OpenAI Agents SDK, a lightweight production-ready Python framework for multi-agent workflows with handoffs, guardrails, tracing, and MCP support.
Last verified:
What is Openai Agents Python?
Openai Agents Python is the OpenAI Agents SDK, a lightweight, production-ready Python framework for building agentic AI applications with multi-agent workflows. It serves as a production upgrade to OpenAI's previous Swarm experimentation, providing a small set of powerful primitives: Agents (LLMs with instructions and tools), Agents as tools/Handoffs (for delegating work between agents), and Guardrails (for input/output validation). The SDK combines these primitives with Python to express complex tool and agent relationships without a steep learning curve.
Key features include a built-in agent loop that handles tool invocation automatically, Python-first design using native language features instead of new abstractions, function tools that turn any Python function into an agent tool with automatic schema generation, MCP server tool calling integration, sessions for persistent memory across turns, human-in-the-loop mechanisms, built-in tracing for debugging and evaluation, sandbox agents for isolated workspaces, and realtime agents for voice applications. The SDK supports both OpenAI models via the Responses API (recommended) and Chat Completions API, plus non-OpenAI provider integration.
Openai Agents Python is designed for Python developers building agentic AI apps, from beginners creating their first text-based agent to experienced developers building complex multi-agent orchestration systems. It works out of the box but allows deep customization, making it suitable for production deployments requiring tracing, evaluation, fine-tuning, and monitoring of agentic flows.
Openai Agents Python pricing
Pricing model: Freemium
The OpenAI Agents SDK itself is free and open-source (pip install openai-agents). The SDK requires an OpenAI API key to use OpenAI models, and you pay for OpenAI API usage based on token consumption. Some optional session backends require additional paid services (Redis, MongoDB Atlas, Dapr cloud). Additional Python packages may need to be installed for extended features: openai-agents[redis] for Redis sessions, openai-agents[mongodb] for MongoDB sessions, openai-agents[dapr] for Dapr sessions, openai-agents[any-llm] for Any-LLM adapter, openai-agents[litellm] for LiteLLM adapter. There is no separate licensing fee for the SDK itself.
Openai Agents Python pros
- Lightweight framework with very few abstractions to learn
- Production-ready upgrade from previous Swarm experimentation
- Built-in agent loop handles tool invocation automatically
- Python-first design uses native language features
- Automatic tool schema generation from Python function signatures
- Pydantic-powered validation for function tool inputs
- Built-in tracing for visualization and debugging
- Support for evaluation and fine-tuning of applications
- Handoffs enable agents to delegate to specialist agents
- Agents as tools pattern for central orchestration
- Multiple session backends (SQLite, Redis, MongoDB, SQLAlchemy, Dapr)
- Input and output guardrails for safety validation
- Tool guardrails for validating function tool calls
- Sandbox agents for isolated workspace execution
- Realtime agents support voice applications with gpt-realtime-2
- Streaming support for progressive response updates
- Human-in-the-loop approval gates for tool calls
- MCP server tool integration built-in
- Support for non-OpenAI model providers
- Async and synchronous runner options
Openai Agents Python cons
- Requires OpenAI API key for OpenAI model usage
- Tracing uploads to OpenAI servers may require separate API key
- Some tool features only work with OpenAI Responses models
- Third-party adapters for non-OpenAI providers are beta/best-effort
- Hosted tool search requires openai>=2.25.0
- Websocket transport requires separate websockets package installation
- Structured output support varies by model provider
- Many LLM providers do not support Responses API yet
- Complex multi-agent patterns require understanding orchestration tradeoffs
- Session memory cannot combine with conversation_id or previous_response_id
Frequently asked questions about Openai Agents Python
What is the OpenAI Agents SDK?
The OpenAI Agents SDK is a lightweight, production-ready Python framework for building agentic AI applications. It enables you to build multi-agent workflows with very few abstractions. The SDK has three core primitives: Agents (LLMs with instructions and tools), Agents as tools/Handoffs (for delegating between agents), and Guardrails (for input/output validation). It is a production upgrade from OpenAI's previous Swarm experimentation.
How do I install the Agents SDK?
Install the SDK using pip: pip install openai-agents. You also need to set an OpenAI API key via the OPENAI_API_KEY environment variable. For extended features like Redis sessions, install with extras: pip install openai-agents[redis]. Create a virtual environment first using python -m venv .venv and activate it before installing.
What models does the SDK support?
The SDK has out-of-the-box support for OpenAI models via the OpenAIResponsesModel (recommended, uses Responses API) and OpenAIChatCompletionsModel (uses Chat Completions API). The default model is gpt-5.4-mini with reasoning.effort=none. It also supports non-OpenAI providers through built-in integration points (set_default_openai_client, ModelProvider, Agent.model) and third-party adapters (Any-LLM, LiteLLM) on a beta basis.
What are handoffs in the Agents SDK?
Handoffs allow agents to delegate the conversation to specialist agents. When a handoff occurs, the delegated agent receives the conversation history and takes over. This is a decentralized pattern where peer agents pass control. You define handoffs on an agent using the handoffs parameter with a list of target agents. Handoffs are different from agents-as-tools, where an orchestrator stays in control and calls specialists as tools.
How do I create function tools?
Use the @function_tool decorator on any Python function. The SDK automatically extracts the tool name from the function name, description from the docstring, and creates a JSON schema from the function signature using Python's inspect module and Pydantic. Functions can be sync or async, and can take any Python types. You can override the name with name_override, and pass context as the first argument if needed.
What tracing capabilities does the SDK provide?
The SDK includes built-in tracing that lets you visualize and debug agentic flows. Traces are uploaded to OpenAI servers and can be viewed in the Trace viewer in the OpenAI Dashboard. The tracing supports evaluation, fine-tuning, and distillation tools from OpenAI. You can disable tracing via set_tracing_disabled() if you don't have an OpenAI API key, or set a separate tracing export API key.
How do sessions work for memory?
Sessions provide built-in session memory to automatically maintain conversation history across multiple agent runs. When enabled, the runner retrieves conversation history before each run and prepends it to input, then stores new items after each run. The SDK provides multiple implementations: SQLiteSession (lightweight, file-backed), RedisSession (shared memory), SQLAlchemySession (production), MongoDBSession, DaprSession, and OpenAIConversationsSession (server-managed).
What are guardrails and how do they work?
Guardrails enable validation of user input and agent output. There are input guardrails (run on initial user input), output guardrails (run on final agent output), and tool guardrails (wrap function tools). Guardrails run a function that returns GuardrailFunctionOutput with a tripwire_triggered flag. If triggered, a GuardrailTripwireTriggered exception is raised and execution halts. Input guardrails support parallel execution (default) or blocking execution before the agent starts.
Can I use the SDK with non-OpenAI models?
Yes, the SDK supports non-OpenAI providers through built-in integration points: set_default_openai_client for global OpenAI-compatible endpoints, ModelProvider for per-run custom providers, and Agent.model for per-agent different providers. Many providers still don't support the Responses API, so you may need to use OpenAIChatCompletionsModel. Third-party adapters (Any-LLM, LiteLLM) are available on a beta basis for more complex routing needs.
What is the difference between Agents SDK and Responses API?
The SDK uses the Responses API by default but adds a higher-level runtime around model calls. Use the Responses API directly when you want to own the loop, tool dispatch, and state handling yourself, or for short-lived workflows. Use the Agents SDK when you want the runtime to manage turns, tool execution, guardrails, handoffs, or sessions, when your agent should produce artifacts or operate across multiple coordinated steps, or when you need a real workspace through Sandbox agents.