OpenAI Agents SDK


This entry is part 1 of 2 in the series OpenAI Agents Projects

The OpenAI Agents SDK is a lightweight and flexible framework for building agentic systems.
It’s intentionally not opinionated — meaning it doesn’t force you into rigid patterns or conventions.
Instead, it gives you freedom to design and structure your agents the way you want, while still simplifying
common tasks such as tool use and data handling.

Many frameworks are “opinionated,” providing pre-defined structures that speed up development but limit flexibility. OpenAI’s approach is different — it automates the repetitive, low-value work (like handling JSON payloads and if-statement logic) while leaving creative control in your hands. You don’t have to do the repetative boilerplate code. This balance between simplicity and freedom makes it a favorite for many developers — Ed included.

Why Start with OpenAI’s SDK?

  • It’s straightforward and beginner-friendly, yet powerful enough for advanced projects.
  • The later frameworks in the course build on similar ideas, so starting here provides a clear foundation.

Core Concepts and Terminology

The OpenAI Agents SDK uses three main terms you’ll encounter often:

  • Agent — A wrapper around an LLM call that performs a specific role or task within your system.
  • Handoff — An interaction or exchange between agents; how they collaborate and pass control.
  • Guardrails — The checks and controls that ensure an agent behaves as intended and stays on track.

Three Steps to Run an Agent

Creating and running an agent follows a simple three-step pattern:

  • 1. Create an instance of the agent — define its role and purpose within your system.
  • 2. Use with trace — this optional step logs your interactions, letting you review activity
    inside OpenAI’s monitoring tools.
  • 3. Run the agent — use runner.run() to execute the agent. Because this is asynchronous,
    you’ll need to call it with await runner.run(). It is a coroutine.

That’s it — three clear steps and three core terms. Together they form the foundation for working with
OpenAI’s flexible, minimal, and powerful agent framework.

OpenAI SDK Docs

Here is the link to the documentation.

Code

We can use code similar to this using VS Code, as I did. With Ed’s course you are able to download the code and put it into a folder of your choice.

# First let's do an import. If you get an Import Error, double check that your Kernel is correct..
from dotenv import load_dotenv

# Next it's time to load the API keys into environment variables
# If this returns false, see the next cell!
load_dotenv(override=True)

# Check the key - if you're not using OpenAI, check whichever key you're using! Ollama doesn't need a key.
import os
openai_api_key = os.getenv('OPENAI_API_KEY')

if openai_api_key:
    print(f"OpenAI API Key exists and begins {openai_api_key[:8]}")
else:
    print("OpenAI API Key not set - please head to the troubleshooting guide in the setup folder")

# And now - the all important import statement
from openai import OpenAI

# And now we'll create an instance of the OpenAI class
# If you're not using OpenAI, you just need to slightly modify this 
openai = OpenAI()

# Create a list of messages in the familiar OpenAI format
messages = [{"role": "user", "content": "What is 2+2?"}]

# And now call it! 
# This uses GPT 4.1 nano, the incredibly cheap model
response = openai.chat.completions.create(
    model="gpt-4.1-nano",
    messages=messages
)
print(response.choices[0].message.content)

Another example is below.

# Week 2 day 1 of the Udemy Course by Ed Donner
# The imports
from dotenv import load_dotenv
from agents import Agent, Runner, trace

# The usual starting point
load_dotenv(override=True)

# Make an agent with name, instructions, model
agent = Agent(name="Jokester", instructions="You are a joke teller", model="gpt-4o-mini")

# Run the joke with Runner.run(agent, prompt) then print final_output
with trace("Telling a joke"):
    result = await Runner.run(agent, "Tell a joke about Autonomous AI Agents")
    print(result.final_output)

# Now go and look at the <a href="https://platform.openai.com/traces" target="_blank">trace</a>. You'll need to log in.

Created collaboratively by Ed Donner, ChatGPT, and Mike Porter.

OpenAI Agents Projects

OpenAI Agents Sales Email

Leave a Reply