Skip to content

Add run configuration inheritance for agent-as-tool workflows #1006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

DanielHashmi
Copy link
Contributor

Problem:
When using the "agents as tools" pattern, tool-agents create their own execution context but don't inherit the parent's run_config.

This creates two problems:
Tool-agents can't access the parent's global model configuration
If tool-agents don't have their own model field set and the parent's run_config.model is unavailable, model resolution fails entirely, causing the orchestrator to fail when calling the tool

Solution:
Modified the agent-as-tool execution to properly handle run_config inheritance while ensuring robust model resolution. This prevents execution failures when tool-agents lack explicit model configuration.

Changes

1. traces.py

  • Added _run_config attribute to both NoOpTrace and TraceImpl classes
  • Added TYPE_CHECKING import for RunConfig type annotation
  • Enables storing run configuration in trace context for cross-agent access

2. run.py

  • Enhanced trace context management to store run configuration
  • Implements two-tier storage: metadata dictionary (preferred) or _run_config attribute (fallback)
  • Ensures sub-agents can access original run configuration during execution

3. agent.py

  • Added get_current_trace import from tracing module
  • Enhanced as_tool() method to retrieve and inherit run configuration from trace context
  • Passes inherited configuration to Runner.run() for consistent behavior

@seratch seratch added enhancement New feature or request feature:core labels Jul 8, 2025
@seratch
Copy link
Member

seratch commented Jul 8, 2025

I am not sure what @rh-openai thinks but here are my initial thoughts on this:

  • I see the need for some use cases but in general, setting the most suitable model for each agent would be recommended rather than sharing the parent's one (again, I hear the need though)
  • I don' think passing via current trace is the best approach for this; enhancing in a more straight-forward way without breaking changes would be ideal

Copy link
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah using the trace to store this data isn't right. What you could do instead, is use a ContextVar. So:

  1. Add a pass_run_config_to_sub_agents: bool param to the RunConfig, just in case people want to enable/disable this behavior
  2. Set the run_config context var from run.py
  3. Read the run_config in the agent as tool
  4. Also ensure that you add tests to make sure the contextvar is unset in all cases (errors, success, cancel etc)

I'm also not entirely sure if it makes sense to have this as part of the SDK, or just something you do. The approach I've taken to as_tool is that if you want customization, you can just create your own tool via:

@function_tool
def run_my_agent(input):
  my_agent = Agent(...)
  result = Runner.run(my_agent)
  return result.foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants