Conversation
When using `Agent.as_tool()` to run parallel subagents that each edit their own branch of a shared document, tools need to know which subagent they belong to in order to read/write the correct branch. The agent is already available at the call site in tool_execution.py and passed to hooks (on_tool_start, on_tool_end), but was not forwarded to the tool itself via ToolContext. This adds an optional `agent` field to ToolContext so tool implementations can inspect the calling agent (name, model, configuration, etc.). The field defaults to None for backward compatibility — ToolContext is a public API and users may construct it directly in tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 31b570f0bd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| context_wrapper, | ||
| tool_call.call_id, | ||
| tool_call=tool_call, | ||
| agent=agent, | ||
| ) |
There was a problem hiding this comment.
Propagate agent in realtime tool invocations too
This adds agent to ToolContext for the standard tool execution path, but realtime tool calls still build ToolContext without an agent (see src/agents/realtime/session.py in _handle_tool_call, where ToolContext(...) is constructed from context/usage only). As a result, tools invoked via RealtimeSession will still see tool_ctx.agent is None, so the new feature does not work in that flow. If the intent is to let tools determine the calling agent, please pass the agent there as well.
Useful? React with 👍 / 👎.
Summary
agentfield toToolContextso tool implementations can inspect which agent is calling themagentthrough fromexecute_function_tool_calls()intool_execution.pyMotivation
When using
Agent.as_tool()to run parallel subagents, we want to designate specific context for each subagent. The problem is that when a tool runs inside a subagent, it needs to know which subagent it belongs to so it can read/write the correct branch.Currently,
on_invoke_toolonly receivesToolContextwith no agent reference. The agent is available in hooks (on_tool_start,on_tool_end) but is not forwarded to the tool itself. There doesn't appear to be an established pattern for a tool to discover whichAgentinstance it's running inside.This PR adds the agent reference directly to
ToolContext, consistent with how it's already passed to hooks.Backward compatibility
The
agentfield defaults toNone, so no existing code breaks:__init__parameter, preserving positional constructor compatibilityfrom_agent_contextgains an optionalagentkeyword argument with defaultNoneToolContextis a public API — users may construct it directly in tests without an agentTest plan
make formatpassesmake lintpassesmake mypypasses (no new errors)agentisNoneby default, populated viafrom_agent_context, and accessible via direct construction🤖 Generated with Claude Code