BUILD · 5 MIN READ

Build with the OpenAI Agents SDK

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

Download example files Code + setup + checks in a ZIP
Jump to a section 9 sections

EXAMPLE USE CASE

Build a support agent that calls a lookup tool

You need a model to choose a read-only operation and answer from its result.

Input
What is the status of A104? plus the lookup_order tool and a configured OpenAI model.
Expected output
A final answer reporting Packed; awaiting courier pickup after the tool result.
  • Success check: Inspect the tool interaction and test A999 as well as A104.
  • Try a failure: An unknown order must return Order not found; a turn limit must terminate a repeated-call loop.

Example scope: A real provider SDK integration using a local order fixture. Running it consumes your configured API usage.

OpenAI Agents SDK exampleGiskard example
SEE HOW THE SYSTEM FITS TOGETHERSELECT A COMPONENT
taskinvokedecisionvalidatedobservationstatecompleteApplication requestIdentity + goalOpenAI Agents SDKAgent or graph runtimeConfigured modelAvailable provider modelAllowed toolsTyped application functionsState + limitsMessages and stopping rulesVerified answerCompare with the result
  1. Resolve the real user and pass a bounded task to the SDK.

  2. The runtime manages the loop or explicit state transitions; it does not replace resource authorization.

  3. Keep provider credentials on the server and select a model supported by the API account.

  4. The example exposes a local fixture. A real tool must check the authenticated user and resource.

  5. Track the current conversation and stop when the turn or recursion budget is reached.

    Result or stopping point
  6. For A104, confirm that the answer reflects the lookup observation and contains no invented dates.

    Result or stopping point
OpenAI Agents SDK

The runtime manages the loop or explicit state transitions; it does not replace resource authorization.

Follow the arrows, then select a component to inspect its responsibility. On mobile, downward arrows connect the next step; bent arrows identify branches and return paths. Select a destination to jump to that component.

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
python3 -m venv .venv
source .venv/bin/activate
python -m pip install openai-agents
export OPENAI_API_KEY="your-api-key"
export OPENAI_MODEL="your-enabled-model-id"
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.

Run and verify

Shell
python agent.py
  • The answer reports that A104 is packed and awaiting courier pickup.
  • Ask about A999: it should report Order not found, without inventing a replacement record.

Troubleshooting and failure tests

  • 401: check which API project owns the key. A consumer chat subscription is separate from API access.
  • Model-not-found: select a model enabled for that API project. A turn-limit error means the loop did not finish within five turns.

Before production

  • Replace the local fixture with a service that enforces authenticated tenant and resource permissions.
  • Add timeouts, bounded retries, tracing redaction, and a regression case for each observed failure.
  • Model calls consume provider API usage when you run this example; this website only displays the code.

Keep exploring

Design tools an agent can use wellAI agent evaluation: frameworks, tools and an exampleA realtime voice agent with OpenAI