How to Build an AI Agent With OpenAI: A Practical Guide
OpenAI's Agents SDK gives you agents, tools, handoffs and guardrails in a small toolkit. Here is how to build your first agent on it, step by step.
On this page
Short answer
An OpenAI agent is an AI agent built on OpenAI models, usually with the OpenAI Agents SDK, that takes a request, uses tools to gather facts or take actions, and hands off to another agent or a human when the task needs it. The SDK keeps the moving parts small, so a first agent is a few dozen lines of code.
What is an OpenAI agent?
An OpenAI agent is a program built around an OpenAI model that can reason, call tools and take actions toward a goal, instead of only answering a single question. The model decides which tool to use, reads the result, and keeps going until the task is done or it needs help.
OpenAI provides the building blocks in two forms: the Agents SDK, a light toolkit for defining agents with tools, handoffs and guardrails, and the Responses API, which handles tool calls, file search and computer use behind the scenes. Most teams start with the Agents SDK, because it shows each step the agent took and makes the flow easy to follow.
An agent built this way is tied to OpenAI models, which is a trade-off worth naming. The benefit is a small, well-documented toolkit with built-in tracing and guardrails. The cost is that moving to another model provider later means rewriting the agent layer. For a team already using OpenAI, that is usually an acceptable trade. See how to build a ChatGPT-powered application for the wider app around an agent.
What does the OpenAI Agents SDK give you?
The OpenAI Agents SDK gives you four building blocks: agents, tools, handoffs and guardrails, plus tracing that records every step. The table below shows what each block does.
| Block | What it does | Example |
|---|---|---|
| Agent | Holds instructions and tools | Support agent, triage agent |
| Tool | Does one job for the agent | Search docs, look up order |
| Handoff | Passes work to another agent | Triage to billing agent |
| Guardrail | Checks input or output | Block harmful requests |
Handoffs are the block teams use most as agents grow. A triage agent can pass a billing question to a billing agent with its own instructions and tools, and the conversation history travels with the handoff. That keeps each agent small and focused, instead of one agent that tries to do everything.
Tracing records each agent step, tool call and handoff, so you can replay a run and see where it went wrong. Guardrails run checks on the input before the agent starts and on the output before it is shown, which is where you block requests the agent must not handle. Both are built in, which is why the SDK is a fast start for a first production agent.
What do you need before you start?
Before you start, you need an OpenAI API key, a clear task, and the tools the agent will call. The task should be narrow: one job with a visible result, such as answering questions over your own documents or drafting replies for a support queue.
Pick the model for the job, not the newest one by default. A smaller, cheaper model may be enough for a triage agent that mostly routes requests, while a stronger model fits an agent that must reason over long documents. Check the current model list and prices on the vendor site before you commit, since both change often.
Prepare the tools as small functions with plain descriptions. A document search tool, an order lookup and a draft writer cover most first agents. Also prepare a test set of real requests with known good answers, and decide which actions need a human to approve them. An agent that can send messages or change records without approval is a risk, not a shortcut. The OpenAI API integration guide covers keys, rate limits and error handling.
How do you build an OpenAI agent, step by step?
You build an OpenAI agent by defining one agent, giving it a few tools, adding a handoff and a guardrail, and testing it on real requests before it faces users.
- Define the agent. Write short instructions: what the agent does, what it must not do, and how it should answer. Keep the instructions to a few sentences.
- Add two or three tools. Start with a document search and one lookup tool. Each tool needs a name and a plain description of when to use it.
- Run it on test requests. Give the agent ten real requests and read the trace for each one. Fix the worst failure before adding anything else.
- Add a handoff. When one request type needs different instructions, create a second agent and let the first hand off to it.
- Add a guardrail. Block requests the agent must not handle, and check outputs before they reach a user.
- Add human approval. Route any action that sends, changes or deletes through a person before it runs.
- Ship behind a flag. Roll out to a small group first, watch the traces and the costs, then widen the rollout.
Keep the agent small. A single agent with three tools that works is worth more than a team of agents that nobody can debug.
How do you keep an OpenAI agent safe?
You keep an OpenAI agent safe with guardrails, approval steps, scoped tools and logging. Safety is a set of controls around the agent, not a property of the model alone.
Guardrails check the input before the agent runs and the output before it is shown. An input guardrail can block requests outside the agent's job, such as asking a support agent for legal advice. An output guardrail can check that a draft reply cites a source document before it goes to a user. Both run as code you control, so the rules are explicit and testable.
Scope the tools so the agent can only do what the task needs. A support agent that reads orders does not need a tool that refunds them. Log every run, including the tools called and the approvals given, so you can review what happened and show it to others.
Set a step limit per run, so a confused agent stops instead of looping. For the wider safety picture, comparing agent frameworks shows how other tools handle the same controls.
Example (hypothetical): an invoice-question agent
Example (hypothetical): a finance team answers 120 questions a month about invoices, and each question takes about 15 minutes to answer by hand. That is 120 times 15 minutes, or 1,800 minutes, which is 30 hours a month.
The team builds an OpenAI agent with three tools: a search over invoice records, a calculator, and a draft writer. The agent answers 90 of the 120 questions, and each answer needs 3 minutes of human review, which is 270 minutes. The other 30 questions go to a person at 15 minutes each, which is 450 minutes. The total is 720 minutes, or 12 hours, against 30 hours before.
The saving is about 18 hours a month, and the review step catches the cases where the agent quoted the wrong invoice. The guardrail blocks any request that asks the agent to change an invoice, and those go straight to a person. The agent never touches money, which is the rule the team set on day one.
What are the common mistakes?
The most common OpenAI agent mistakes are writing instructions that are too long, giving the agent tools it does not need, and skipping the approval step for actions that matter.
- Long instructions. An agent with a page of instructions will ignore half of them. Keep instructions short, and put details in tools and guardrails instead.
- Too many tools. An agent with ten tools picks the wrong one. Start with two or three and add one only when a real request shows the gap.
- No guardrails. Without input and output checks, the agent will eventually answer a request it should have refused.
- Write access without approval. An agent that can send or change records without a human check will eventually do something expensive.
- Ignoring the traces. The trace is how you debug an agent. If nobody reads it, the same failure will keep happening.
- No cost limit. Each run costs model calls. Set a step limit and watch the cost per run during the first weeks.
Key takeaways
- The Agents SDK gives you agents, tools, handoffs and guardrails
- Short instructions and a few well-described tools beat a long setup
- Guardrails check input and output, and approval gates real actions
- Tracing shows every step, so read it when something goes wrong
- Ship behind a flag and widen the rollout after watching real runs
Frequently asked questions
What is the difference between the Agents SDK and the Responses API?
The Responses API is the underlying API for tool calls, file search and computer use, while the Agents SDK is a toolkit built on top of it that adds agents, handoffs, guardrails and tracing. Use the SDK when you want those building blocks ready-made, and the Responses API directly when you want to build your own agent loop.
Do I need the Agents SDK to build an agent on OpenAI?
No. You can build an agent with plain API calls, a loop that calls tools and a prompt that drives the model. The SDK saves you that plumbing and adds tracing and guardrails, so most teams start with it and drop down to the API only when they need custom behaviour.
Can an OpenAI agent use tools outside OpenAI?
Yes. A tool is a function you write, so it can call your own API, a database or any other service. The model decides when to call it from its description. Keep tools narrow and read-only where possible, and add approval for any tool that writes or sends.
How do handoffs work between agents?
A handoff passes the conversation to another agent with its own instructions and tools. The first agent decides the request fits the second agent better, and the history travels with the handoff so the user does not repeat themselves. Use handoffs to keep each agent small and focused.
What does an OpenAI agent cost to run?
It costs more than a single chat completion, because each turn of the agent loop is another model call, and a run can take several calls plus tool costs. Track the cost per run during testing and set a step limit. Prices change often, so check the vendor site before budgeting.
Can I move an OpenAI agent to another model provider later?
Not without work. The Agents SDK is built around OpenAI models, so moving means rewriting the agent layer for another provider or framework. If model choice matters to you, pick a provider-neutral framework from the start rather than the SDK.