BUILD · 5 MIN READ
Build with the OpenAI Agents SDK
Define an agent, expose a small tool, and let the runner manage the loop.
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.
Resolve the real user and pass a bounded task to the SDK.
The runtime manages the loop or explicit state transitions; it does not replace resource authorization.
invokeTo 03 · Configured modelvalidatedTo 04 · Allowed toolscompleteTo 06 · Verified answerKeep provider credentials on the server and select a model supported by the API account.
decisionBack to 02 · OpenAI Agents SDKThe example exposes a local fixture. A real tool must check the authenticated user and resource.
observationBack to 02 · OpenAI Agents SDKTrack the current conversation and stop when the turn or recursion budget is reached.
Result or stopping pointFor A104, confirm that the answer reflects the lookup observation and contains no invented dates.
Result or stopping point
The runtime manages the loop or explicit state transitions; it does not replace resource authorization.
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_KEYandOPENAI_MODELin your shell, choosing a model available to your API project. - Save the example as
agent.pyand runpython agent.py.
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"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
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.