$npx -y skills add mjunaidca/mjs-agent-skills --skill scaffolding-openai-agentsBuilds AI agents using OpenAI Agents SDK with async/await patterns and multi-agent orchestration. Use when creating tutoring agents, building agent handoffs, implementing tool-calling agents, or orchestrating multiple specialists. Covers Agent class, Runner patterns, function too
| 1 | # Scaffolding OpenAI Agents |
| 2 | |
| 3 | Build production AI agents using OpenAI Agents SDK with native async/await patterns. |
| 4 | |
| 5 | ## Quick Start |
| 6 | |
| 7 | ```bash |
| 8 | # Project setup |
| 9 | mkdir my-agent && cd my-agent |
| 10 | python -m venv .venv && source .venv/bin/activate |
| 11 | pip install openai-agents |
| 12 | |
| 13 | # Set API key |
| 14 | export OPENAI_API_KEY=sk-... |
| 15 | ``` |
| 16 | |
| 17 | ```python |
| 18 | # main.py |
| 19 | import asyncio |
| 20 | from agents import Agent, Runner |
| 21 | |
| 22 | agent = Agent( |
| 23 | name="Python Tutor", |
| 24 | instructions="You help students learn Python. Explain concepts clearly with examples." |
| 25 | ) |
| 26 | |
| 27 | async def main(): |
| 28 | result = await Runner.run(agent, "Explain list comprehensions") |
| 29 | print(result.final_output) |
| 30 | |
| 31 | asyncio.run(main()) |
| 32 | ``` |
| 33 | |
| 34 | ## Agent Configuration |
| 35 | |
| 36 | ### Basic Agent |
| 37 | |
| 38 | ```python |
| 39 | from agents import Agent |
| 40 | |
| 41 | tutor = Agent( |
| 42 | name="Python Tutor", |
| 43 | instructions="""You are an expert Python tutor. |
| 44 | Explain concepts clearly with examples. |
| 45 | Ask clarifying questions when needed. |
| 46 | Provide practice exercises after explanations.""", |
| 47 | model="gpt-4o" |
| 48 | ) |
| 49 | ``` |
| 50 | |
| 51 | ### With Model Settings |
| 52 | |
| 53 | ```python |
| 54 | from agents import Agent, ModelSettings |
| 55 | |
| 56 | agent = Agent( |
| 57 | name="Creative Writer", |
| 58 | instructions="Write creative stories based on prompts.", |
| 59 | model="gpt-4o", |
| 60 | model_settings=ModelSettings( |
| 61 | temperature=0.9, |
| 62 | max_tokens=2000 |
| 63 | ) |
| 64 | ) |
| 65 | ``` |
| 66 | |
| 67 | ### With Structured Output |
| 68 | |
| 69 | ```python |
| 70 | from pydantic import BaseModel |
| 71 | from agents import Agent |
| 72 | |
| 73 | class CodeReview(BaseModel): |
| 74 | issues: list[str] |
| 75 | suggestions: list[str] |
| 76 | score: int |
| 77 | |
| 78 | reviewer = Agent( |
| 79 | name="Code Reviewer", |
| 80 | instructions="Review Python code for issues and improvements.", |
| 81 | output_type=CodeReview # Forces structured JSON output |
| 82 | ) |
| 83 | ``` |
| 84 | |
| 85 | ## Runner Patterns |
| 86 | |
| 87 | ### Async Run (Primary) |
| 88 | |
| 89 | ```python |
| 90 | import asyncio |
| 91 | from agents import Agent, Runner |
| 92 | |
| 93 | async def main(): |
| 94 | agent = Agent(name="Helper", instructions="Be helpful") |
| 95 | |
| 96 | # Single query |
| 97 | result = await Runner.run(agent, "What is Python?") |
| 98 | print(result.final_output) |
| 99 | |
| 100 | # With conversation history |
| 101 | messages = [ |
| 102 | {"role": "user", "content": "My name is Alex"}, |
| 103 | {"role": "assistant", "content": "Nice to meet you, Alex!"}, |
| 104 | {"role": "user", "content": "What's my name?"} |
| 105 | ] |
| 106 | result = await Runner.run(agent, messages) |
| 107 | print(result.final_output) # "Your name is Alex" |
| 108 | |
| 109 | asyncio.run(main()) |
| 110 | ``` |
| 111 | |
| 112 | ### Sync Run (Simple Scripts) |
| 113 | |
| 114 | ```python |
| 115 | from agents import Agent, Runner |
| 116 | |
| 117 | agent = Agent(name="Helper", instructions="Be helpful") |
| 118 | result = Runner.run_sync(agent, "Hello!") |
| 119 | print(result.final_output) |
| 120 | ``` |
| 121 | |
| 122 | ### Streaming Run |
| 123 | |
| 124 | ```python |
| 125 | import asyncio |
| 126 | from agents import Agent, Runner |
| 127 | |
| 128 | async def main(): |
| 129 | agent = Agent(name="Storyteller", instructions="Tell engaging stories") |
| 130 | |
| 131 | result = Runner.run_streamed(agent, "Tell me a short story") |
| 132 | |
| 133 | async for event in result.stream_events(): |
| 134 | if hasattr(event, 'delta'): |
| 135 | print(event.delta, end='', flush=True) |
| 136 | |
| 137 | print() # Newline at end |
| 138 | |
| 139 | asyncio.run(main()) |
| 140 | ``` |
| 141 | |
| 142 | ### Conversation Continuation |
| 143 | |
| 144 | ```python |
| 145 | async def chat_session(): |
| 146 | agent = Agent(name="Tutor", instructions="You are a Python tutor") |
| 147 | |
| 148 | # First turn |
| 149 | result1 = await Runner.run(agent, "Explain decorators") |
| 150 | print(f"Tutor: {result1.final_output}") |
| 151 | |
| 152 | # Continue conversation |
| 153 | messages = result1.to_input_list() + [ |
| 154 | {"role": "user", "content": "Show me an example"} |
| 155 | ] |
| 156 | result2 = await Runner.run(agent, messages) |
| 157 | print(f"Tutor: {result2.final_output}") |
| 158 | ``` |
| 159 | |
| 160 | ## Function Tools |
| 161 | |
| 162 | ### Basic Tool |
| 163 | |
| 164 | ```python |
| 165 | from agents import Agent, function_tool |
| 166 | |
| 167 | @function_tool |
| 168 | def get_current_time() -> str: |
| 169 | """Get the current time.""" |
| 170 | from datetime import datetime |
| 171 | return datetime.now().strftime("%H:%M:%S") |
| 172 | |
| 173 | @function_tool |
| 174 | def calculate(expression: str) -> float: |
| 175 | """Calculate a mathematical expression. |
| 176 | |
| 177 | Args: |
| 178 | expression: A valid Python math expression like "2 + 2" or "10 * 5" |
| 179 | """ |
| 180 | return eval(expression) # Use safe_eval in production |
| 181 | |
| 182 | agent = Agent( |
| 183 | name="Assistant", |
| 184 | instructions="Help with calculations and time queries.", |
| 185 | tools=[get_current_time, calculate] |
| 186 | ) |
| 187 | ``` |
| 188 | |
| 189 | ### Async Tool |
| 190 | |
| 191 | ```python |
| 192 | import httpx |
| 193 | from agents import Agent, function_tool |
| 194 | |
| 195 | @function_tool |
| 196 | async def fetch_weather(city: str) -> str: |
| 197 | """Fetch current weather for a city. |
| 198 | |
| 199 | Args: |
| 200 | city: The city name to get weather for |
| 201 | """ |
| 202 | async with httpx.AsyncClient() as client: |
| 203 | response = await client.get( |
| 204 | f"https://wttr.in/{city}?format=3" |
| 205 | ) |
| 206 | return response.text |
| 207 | |
| 208 | agent = Agent( |
| 209 | name="Weather B |