Tool Context
ToolContext
dataclass
Bases: RunContextWrapper[TContext]
The context of a tool call.
Source code in src/agents/tool_context.py
tool_name
class-attribute
instance-attribute
tool_name: str = (
_assert_must_pass_tool_name()
if tool_name is _MISSING
else cast(str, tool_name)
)
The name of the tool being invoked.
tool_arguments
class-attribute
instance-attribute
tool_arguments: str = (
_assert_must_pass_tool_arguments()
if tool_arguments is _MISSING
else cast(str, tool_arguments)
)
The raw arguments string of the tool call.
tool_call_id
class-attribute
instance-attribute
tool_call_id: str = (
_assert_must_pass_tool_call_id()
if tool_call_id is _MISSING
else cast(str, tool_call_id)
)
The ID of the tool call.
tool_call
class-attribute
instance-attribute
The tool call object associated with this invocation.
agent
class-attribute
instance-attribute
agent: AgentBase[Any] | None = agent
The active agent for this tool call, when available.
context
instance-attribute
The context object (or None), passed by you to Runner.run()
usage
class-attribute
instance-attribute
The usage of the agent run so far. For streamed responses, the usage will be stale until the last chunk of the stream is processed.
tool_input
class-attribute
instance-attribute
Structured input for the current agent tool run, when available.
__init__
__init__(
context: TContext,
usage: Usage | object = _MISSING,
tool_name: str | object = _MISSING,
tool_call_id: str | object = _MISSING,
tool_arguments: str | object = _MISSING,
tool_call: ResponseFunctionToolCall | None = None,
*,
agent: AgentBase[Any] | None = None,
turn_input: list[TResponseInputItem] | None = None,
_approvals: dict[str, _ApprovalRecord] | None = None,
tool_input: Any | None = None,
) -> None
Preserve the v0.7 positional constructor while accepting new context fields.
Source code in src/agents/tool_context.py
from_agent_context
classmethod
from_agent_context(
context: RunContextWrapper[TContext],
tool_call_id: str,
tool_call: ResponseFunctionToolCall | None = None,
agent: AgentBase[Any] | None = None,
) -> ToolContext
Create a ToolContext from a RunContextWrapper.
Source code in src/agents/tool_context.py
is_tool_approved
Return True/False/None for the given tool call.
Source code in src/agents/run_context.py
approve_tool
approve_tool(
approval_item: ToolApprovalItem,
always_approve: bool = False,
) -> None
Approve a tool call, optionally for all future calls.
Source code in src/agents/run_context.py
reject_tool
reject_tool(
approval_item: ToolApprovalItem,
always_reject: bool = False,
) -> None
Reject a tool call, optionally for all future calls.
Source code in src/agents/run_context.py
get_approval_status
get_approval_status(
tool_name: str,
call_id: str,
*,
existing_pending: ToolApprovalItem | None = None,
) -> bool | None
Return approval status, retrying with pending item's tool name if necessary.