Skip to content

Expose RunItems in RunContextWrapper #2030

@varunursekar

Description

@varunursekar

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions