The OpenAI Agents SDK packages an agent’s model, instructions, tools, handoffs, guardrails, and output contract, while a runner executes the model-and-tool loop. Your first program should use one focused agent with no tools, prove authentication and result handling, inspect its trace, and only then add authority.
You will create an isolated project, install openai-agents, run one asynchronous turn, distinguish Agent from Runner, and diagnose the common setup failures.
Check current models, access, and pricing in the official console. Model identifiers in documentation can change; this example uses the SDK default rather than freezing one into the lesson.
Do not put the key in source code or shell history on a shared machine. A production service should obtain it from a secrets manager.
first_agent.py:
Run it:
Agent is configuration. Runner.run() performs the loop and returns a result. final_output is the final user-facing value; later chapters inspect richer history, usage, interruptions, and state.
Check four pieces of evidence:
Exact wording may vary; the behavioral assertions should not.
Trace this run: The input enters Runner, the model produces a final answer without a tool call, and the runner returns. There is one application turn and no delegated authority.
Test a request such as “refund me now.” The agent has no refund tool, so it cannot perform the action. It should state its limitation instead of pretending. Capability comes from tools and runtime access, not from confident language.
Use centralized configuration, pin and test dependency upgrades, set request deadlines, attach correlation/tenant identifiers through local run context, redact traces where required, and translate provider errors into stable domain outcomes. Keep the first production slice read-only.
Wrap the run in an application function that accepts a request ID and returns a typed domain outcome. Add a timeout and a test using a fake adapter—without calling the live API.