Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Openai Openai agents python Basic Agent Execution

From Leeroopedia
Knowledge Sources
Domains AI_Agents, LLMs, Orchestration
Last Updated 2026-02-11 14:00 GMT

Overview

End-to-end process for creating a single AI agent with instructions and executing it via the Runner to obtain a text response.

Description

This workflow covers the fundamental usage pattern of the OpenAI Agents SDK. It demonstrates how to define an Agent with a name, system instructions, and an optional model selection, then execute it against user input using the Runner class. The Runner orchestrates the turn-based execution loop, calling the model and returning a RunResult containing the agent's final output. This is the simplest possible agent workflow and serves as the foundation for all other patterns in the SDK.

Usage

Execute this workflow when you need to create a straightforward AI assistant that responds to user prompts without tools, handoffs, or streaming. This is appropriate for simple question-answering, text generation, or any task where a single model call with instructions suffices.

Execution Steps

Step 1: Agent Definition

Create an Agent instance by specifying a name and system instructions. The instructions define the agent's behavior and persona. Optionally configure the model (defaults to the SDK's default model) and output type (defaults to plain text).

Key considerations:

  • The name is used for tracing and logging identification
  • Instructions can be a static string or a callable that dynamically generates the prompt
  • An optional output_type (Pydantic model) can enforce structured JSON output

Step 2: Runner Invocation

Pass the agent and user input to Runner.run(), which is an async method that orchestrates the agent execution loop. The Runner resolves the model provider, prepares the input payload, and sends it to the model API.

Key considerations:

  • Runner.run() is async and must be awaited
  • A synchronous wrapper Runner.run_sync() is also available
  • The input can be a plain string or a list of input items for multi-turn conversations

Step 3: Model Execution

The Runner's internal run loop sends the agent's instructions and user input to the configured model (via the Responses API or Chat Completions API). The model generates a response, which may include text output or tool calls. For this basic workflow, only text output is expected.

Key considerations:

  • The default API is OpenAI Responses API
  • Chat Completions API is also supported via OpenAIChatCompletionsModel
  • LiteLLM can be used for third-party model providers

Step 4: Result Extraction

The Runner returns a RunResult object containing the final output, new items generated during execution, and metadata such as the last agent that ran. Extract the final_output property to get the agent's text response.

Key considerations:

  • result.final_output contains the agent's response as a string (or structured object if output_type was set)
  • result.new_items contains all items generated during the run
  • result.last_agent identifies which agent produced the final output

Execution Diagram

GitHub URL

Workflow Repository