BUILD · 5 MIN READ

Build with the OpenAI Agents SDK

Define an agent, expose a small tool, and let the runner manage the loop.

Where it fits

The Agents SDK supplies an agent abstraction and a runner, with tools and patterns for coordinating agents. Start with one agent and a local lookup tool so you can inspect the evidence behind the answer.

Prepare the example

Use Python in a virtual environment. Install openai-agents. Set OPENAI_API_KEY and OPENAI_MODEL in your shell, choosing a model available to your API project. Save the example as agent.py and run python agent.py.

Shell · install in a virtual environment
pip install openai-agents
Python · agent.py
import asyncio
import os
from agents import Agent, Runner
from agents.decorators import tool

@tool
def lookup_order(order_id: str) -> str:
    """Read an order status from a local demonstration fixture."""
    orders = {"A104": "Packed; awaiting courier pickup"}
    return orders.get(order_id, "Order not found")

agent = Agent(
    name="Order assistant",
    model=os.environ["OPENAI_MODEL"],
    instructions=(
        "Use lookup_order for status questions. "
        "Report only the returned status; do not invent dates."
    ),
    tools=[lookup_order],
)

async def main():
    result = await Runner.run(
        agent, "What is the status of A104?", max_turns=5
    )
    print(result.final_output)

if __name__ == "__main__":
    asyncio.run(main())

Educational example. Uses your configured environment and credentials; no paid model call is made by this website.

Read the loop

The decorator exposes a function as a tool. The runner sends the task and tool schema to the model, executes requested tools, and returns a final output. The fixture contains one sample order; the model should ground its answer in that returned status.

Extend it deliberately

Replace the fixture with an authenticated service call. Add resource-level authorization in that service, handle timeouts, and test a nonexistent order. Capture a trace and compare the final answer with the tool observation.

Keep exploring

Design tools an agent can use wellHow do you know your agent works?A realtime voice agent with OpenAI