Sitemap

Member-only story

[context] passing in open AI agents

3 min readJun 13, 2025

--

while playing with OpenAI agents we can also set context for all agents/tools & handoffs etc by passing context where context is a dependency-injection tool and it can be passed to all the layers if passed in runner object. We can also manage to flow context manually by creating class as shown below. for context to pass to all layers we need “dataclass” .

lets take a small example again ( refer this )

here i am using same weather agent which have tools to fetch weather of city and activities in city . Now what if i want to set context and pass to runner ->agents -> tools flow

Create a Weather Agent class with Context enabled

class WeatherAgentCon(Agent,UserContext):
def __init__(self,context: UserContext):
self.context = context
instructions = (f"check {context.city} weather. Search activities in {context.state} state and in reply you should also say "
f"Hi!! to {context.name}")


# Just pass the function references, not their output
super().__init__(
name='WeatherAgent',
model="gpt-4.1-nano",
handoff_description="Specialist agent for Weather & Activities related questions",
instructions=instructions,
tools=[get_weather_cont, get_activities]
)

def display_info(self):
print(f"Agent name: {self.name}")
print(f"Instructions: {self.instructions}")

--

--

Karan Sharma
Karan Sharma

Written by Karan Sharma

QA Engineer | Learner | Exploring how AI really works | 15+ years in Software Engineering and still curious every day.