Skip to content

OpenAI Agents SDK

Build an agent that answers questions with your Python functions. Give it an order lookup, and the Codex runtime in your Python app handles the calls between the model and your tool.

Installation

Install v1 in your project's Python environment. If you are starting fresh, you can create a virtual environment. Set OPENAI_API_KEY to your API key and OPENAI_MODEL to an OpenAI model ID your project can use.

Set the API key and model

Replace your_api_key_here with your OpenAI API key.

macOS or Linux

export OPENAI_API_KEY="your_api_key_here"
export OPENAI_MODEL="gpt-5.4"

Windows PowerShell

$env:OPENAI_API_KEY = "your_api_key_here"
$env:OPENAI_MODEL = "gpt-5.4"

Run an agent with a Python tool

Save this order lookup as orders.py.

import asyncio
import os

from openai_agents import Agent, Runner
from openai_agents.codex import function_tool


@function_tool
def delivery_date(order_id: str) -> str:
    """Look up the expected delivery date for an order."""
    return {"A12": "Friday"}.get(order_id, "Unknown order")


agent = Agent(
    model=os.environ["OPENAI_MODEL"],
    instructions="Use delivery_date to check the order before you answer.",
    tools=[delivery_date],
)
result = asyncio.run(Runner.run(agent, "When will order A12 arrive?"))
print(result.output_text)

The run makes billable model requests. Run the script.

python orders.py

Check that the answer reports Friday as the delivery day. To see each step, follow the order lookup through one turn.

Next steps

  • Give an agent access to files or commands by choosing where code runs.
  • Continue a conversation or resume it later with sessions and checkpoints.
  • For a v0 workflow, start with Compare v0 and v1. If it uses a Python model adapter such as LiteLLM, keep it on the supported Classic subpackage, openai_agents.classic, while you migrate.

Why Codex?

We chose the Codex harness so your Python agent can use the same loop Codex uses to carry long tasks through multiple steps.

  • React to tool results. An agent can investigate a failing test, use the failure output to guide an edit, and rerun the test.
  • Keep working on long tasks. Codex can compact a growing conversation before its history exceeds the model's context window.
  • Pick up where work stopped. Your app can save a checkpoint and resume the conversation on another worker.

As SDK releases adopt improvements to the Codex loop, your app can use them without rewriting those improvements in Python. Your app controls its tools, data, access checks, and storage. See what runs where.

Choose an agent product

Use the Agents SDK to build your own agent on the Codex runtime. Use the Codex SDK to run the Codex coding agent from your app.

For example, suppose a CI job fails.

  • With the Agents SDK, create an Agent with your instructions, Python tools for reading logs, retry rules, and approval steps.
  • With the Codex SDK, start a Codex thread in the repository and ask Codex to inspect the failure, edit the code, and rerun tests.
Product When to use it Who manages the runtime Example
Agents SDK Define and host your own agent and tools in Python. Your Python process runs the shared Codex engine. Look up a customer's order with a Python tool.
Agents API Configure your own agent for OpenAI to run. OpenAI runs the loop and maintains session state. Investigate an alert and resume the same session.
Codex SDK Give the existing Codex coding agent a task. You run Codex locally through its Python library and App Server. Diagnose a failing CI job in a repository.

To call a model directly or implement your own agent loop, use the Responses API.

Running on premises. Choose the Agents SDK to run the agent loop on your infrastructure. By default, the v1 Codex runtime sends model requests to the OpenAI Responses API. Select an OpenAI model by ID. With the Agents API, you can host the sandbox, but OpenAI runs the loop and manages session state.

Zero Data Retention (ZDR). The Agents API does not support ZDR at this time. Use the Agents SDK with ZDR enabled for an approved organization or project and eligible endpoints and features. For Classic, disable Python tracing before a ZDR run with OPENAI_AGENTS_DISABLE_TRACING=1. See the v0 tracing guide. This setting does not configure Codex telemetry.