Welcome to the first lesson of our course, Getting Started with OpenAI Agents in Python. In this lesson, you will take your very first steps with the OpenAI Agents SDK. This course is designed for learners who want to build intelligent, task-oriented agents using Python. You do not need any prior experience with the SDK, and we will guide you through each concept with clear explanations and practical examples.
By the end of this lesson, you will know what an OpenAI agent is, how it differs from a simple chat-based language model, and how to create and run your own agent using the OpenAI Agents SDK. This foundational knowledge will prepare you for more advanced topics and hands-on exercises in the rest of the course.
An agent, in this context, is more than just a chatbot. While traditional chat LLMs like the first versions of ChatGPT simply respond to prompts with text, agents are designed to perform specific tasks, follow detailed instructions, and even use tools or delegate work to other agents.
For example, a chat LLM might answer, “What’s the weather like in Paris?” with a general response based on its training data. An agent, on the other hand, could be set up as a travel assistant, given instructions to always provide up-to-date information, and even use external APIs to fetch the latest weather. Agents are configurable, can be given roles, and can be equipped with tools and safety checks, making them much more powerful and reliable for real-world applications.
The OpenAI Agents SDK is a Python library designed to help you easily build, manage, and run AI agents powered by large language models. With this SDK, you can create agents that do much more than just chat—they can reason, use tools, interact with other agents, and perform complex, multi-step tasks in a structured way. The SDK's simple and flexible design makes it easy to prototype and deploy agentic applications quickly. For more information, you can visit the official OpenAI Agents SDK Documentation.
Throughout this course, you'll be working in the CodeSignal environment where everything is already set up for you—no installation or API key configuration needed. You can focus entirely on learning and experimenting with the code.
If you'd like to run the code examples on your own machine later, you'll need to install the library and set your OpenAI API key. To install the SDK, you can use the following pip command:
After installing, set your OpenAI API key as an environment variable. The command you use depends on your operating system:
Once the environment is set, the SDK will automatically detect your API key when running the code, establishing a successful connection to the OpenAI API. Remember, none of these setup steps are necessary while working through this course—the CodeSignal environment already has a valid API key configured for you.
A key feature of the OpenAI Agents SDK is the agent loop. This is the process that allows an agent to handle complex tasks step by step. When you give an agent an input, it processes the input using its instructions and the underlying language model. If the agent needs to use a tool or hand off the task to another agent, the loop takes care of that, running each step until a final answer is ready.
Think of the agent loop as a conversation between the agent and its environment. The agent keeps working — using tools, asking for help, or refining its answer — until it can give you a complete response. This makes agents much more flexible and capable than simple chatbots, which only respond once and cannot take further actions.
Let's see how to create a simple agent using the OpenAI Agents SDK. The main class you will use is called Agent
.
In this code, we create an agent named “Travel Genie.” The instructions tell the agent to act as a travel assistant, be friendly, and give personalized suggestions. At its most basic, you need to provide two key parameters:
- The
name
parameter serves as a unique identifier for your agent, making it easier to reference when working with multiple agents. - The
instructions
parameter acts as the system prompt, defining the agent's role and behavior guidelines.
When you create an agent with just these parameters, it will use the default language model configured in the SDK.
For more control over your agent's capabilities, you can specify which language model it should use:
By default, the SDK will use models provided by OpenAI. For example, "gpt-4.1"
is a cost-effective model optimized for agentic workflows. You can also specify other OpenAI models, such as "gpt-4o"
for low latency and strong performance, or "o4-mini"
and "o3"
for advanced reasoning and complex problem-solving. The SDK is flexible and also allows you to use models from other providers, like Anthropic or Google, through integrations such as LiteLLM. However, in this course, we will focus exclusively on using OpenAI models to keep things simple and consistent.
For even more precise control over how your agent generates responses, you can adjust various model settings with the model_settings
parameter:
The model_settings
parameter accepts a ModelSettings
object where you can configure:
temperature
: Controls randomness in responses. The valid range is 0 to 2. Lower values (e.g., 0.2) produce more focused, deterministic outputs, while higher values (e.g., 1.0 or above) generate more creative, varied responses.max_tokens
: Caps the length of the agent's response to the specified number of tokens. This is primarily used to control costs, since API usage is billed per token. The model is not aware of this limit while generating its output, so if the response is cut off, it may end abruptly or mid-sentence.
These settings allow you to tune your agent's behavior to match your specific application requirements.
Once you have defined your agent, you can run it using the Runner
class. The OpenAI Agents SDK provides multiple ways to run your agent: synchronously, asynchronously, or even with streaming responses. In this first lesson, we'll focus on the synchronous approach using the run_sync
method, which lets you interact with the agent in a blocking way, meaning your code will wait for the agent to finish before moving on.
The run_sync
method requires two main parameters:
starting_agent
: theAgent
instance you want to run.input
: the initial input or prompt for the agent, typically a string representing the user's message.
Here is how you can run the agent with a user prompt:
In this example, we ask the agent to recommend a destination for adventure seekers. The method takes the agent and the input prompt, runs the agent loop, and returns the result when the agent is done.
When you run an agent, the Runner executes what's known as the "agent loop":
- The Runner calls the language model for the current agent with the provided input
- The language model produces its output
- If the language model returns a
final_output
, the loop ends and returns the result - If the language model delegates to another agent (does a handoff), the Runner updates the current agent and input, then re-runs the loop
- If the language model needs to use tools, the Runner executes those tool calls, appends the results, and re-runs the loop
- This process continues until a final output is reached or the maximum number of turns is exceeded
This loop structure is what gives agents their power to handle complex, multi-step tasks that might require different capabilities or external information.
After running the agent, you will get a result object. The most important part of this object is the final_output
attribute, which contains the agent’s final answer.
To see the output, you can print it like this:
If you run the full example, the output might look like this:
This output shows how the agent follows its instructions, providing an enthusiastic and tailored recommendation. In real-world applications, you can use this output to power chatbots, virtual assistants, or any system that needs intelligent, role-based responses.
In this lesson, you learned what OpenAI agents are and how they differ from simple chat LLMs. You explored the agent loop, which allows agents to handle complex tasks step by step. You also saw how to define an agent with a specific role, run it synchronously with a user prompt, and extract the final output.
You are now ready to practice these skills with hands-on exercises. In the next part of the course, you will get to experiment with creating your own agents and running them in different scenarios. Remember, on CodeSignal, all the necessary libraries are pre-installed, so you can focus on learning and building without worrying about setup. Let’s get started!
