Skip to content

feat: add agent field to ToolContext#2439

Closed
PCerles wants to merge 1 commit intoopenai:mainfrom
PCerles:feat/add-agent-to-tool-context
Closed

feat: add agent field to ToolContext#2439
PCerles wants to merge 1 commit intoopenai:mainfrom
PCerles:feat/add-agent-to-tool-context

Conversation

@PCerles
Copy link

@PCerles PCerles commented Feb 8, 2026

Summary

  • Adds an optional agent field to ToolContext so tool implementations can inspect which agent is calling them
  • Passes the agent through from execute_function_tool_calls() in tool_execution.py
  • Adds test coverage for the new field

Motivation

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_tool only receives ToolContext with 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 which Agent instance 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 agent field defaults to None, so no existing code breaks:

  • It is a keyword-only __init__ parameter, preserving positional constructor compatibility
  • from_agent_context gains an optional agent keyword argument with default None
  • ToolContext is a public API — users may construct it directly in tests without an agent

Test plan

  • make format passes
  • make lint passes
  • make mypy passes (no new errors)
  • All existing tests pass
  • New tests verify agent is None by default, populated via from_agent_context, and accessible via direct construction

🤖 Generated with Claude Code

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>
@PCerles PCerles marked this pull request as ready for review February 8, 2026 17:06
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 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".

Comment on lines 820 to 824
context_wrapper,
tool_call.call_id,
tool_call=tool_call,
agent=agent,
)

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@PCerles PCerles closed this Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant