Instructions that change per run
The customer's name, their plan, today's date. None of that belongs in a fixed string, because it is different every run.
Instructions can be a function instead. The SDK calls it at the start of each run and uses whatever it returns.
def instructions(context, agent):
"""Built fresh for every run, so it can use anything you know."""
return f"You are {agent.name}. The customer is {context.context['name']}. Be brief."Two arguments arrive: the run's context, which is anything you passed in, and the agent itself, so a shared function can serve several agents and still name the right one.
agent = Agent(
name="Support",
instructions=instructions,
model=PretendModel(["Hello Asha, your order is on its way."]),
)result = await Runner.run(agent, "any news?", context={"name": "Asha"})
print(result.final_output)The thing to notice is where Asha came from. Not the question, and not the agent. It was passed to Runner.run as context and the instruction function reached for it.
Why not just put it in the question
You could paste the customer's details into every message, and plenty of code does. It costs tokens on every turn, it mixes what the user said with what you know, and it means every tool that wants the customer's name has to dig it back out of the text.
Passing it as context keeps the two apart, and the next lesson shows tools reading the same object.
- Add the plan to the context and mention it in the instructions.
- Print the instruction string inside the function before returning it.
- Run the same agent twice with different contexts.
Little by little, you're building something great.