-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
Currently RunContextWrapper is meant to store context that is not exposed to LLMs and can be used to control the behaviour of tools. The default AgentRunner builds up RunItems iteratively and only exposes them to the user via RunResult once the tool loop is complete. Requesting for intermediate RunItems to be exposed via the RunContextWrapper to allow for alteration of tool behaviour based on the trace generated so far (e.g. number of turns, number of tool calls). The immediate value unlock is to pass any part of the generated context to sub-agents. For example, at step i, the main agent can fork its trajectory N ways and e.g. take the best output; importantly this is done without the main agent having to copy its context into the tool kwargs.
Proposed structure:
@dataclass
class RunContextWrapper(Generic[TContext]):
context: TContext
usage: Usage = field(default_factory=Usage)
run_items: list[RunItem]This will allow for usage such as:
def my_sub_agent(cts: RunContextWrapper, prompt: str):
""" My sub agent only needs the last 2 messages from my main trace"""
input = [x.to_input_item() for x in ctx.run_items[-2:]] + [{"role" : "user", "content" : prompt}]
return await Runner.run(Agent(...), input=input)