Skip to content

Commit 63041aa

Browse files
committed
refactor: use presence of usage_limits parameter to determine tool counting
1 parent 5402c06 commit 63041aa

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

pydantic_ai_slim/pydantic_ai/_agent_graph.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ async def process_function_tools( # noqa: C901
701701
output_parts.append(part)
702702
else:
703703
try:
704-
result_data = await tool_manager.handle_call(call, usage_limits=ctx.deps.usage_limits)
704+
result_data = await tool_manager.handle_call(call)
705705
except exceptions.UnexpectedModelBehavior as e:
706706
ctx.state.increment_retries(ctx.deps.max_result_retries, e)
707707
raise e # pragma: lax no cover
@@ -764,9 +764,9 @@ async def process_function_tools( # noqa: C901
764764
calls_to_run,
765765
deferred_tool_results,
766766
ctx.deps.tracer,
767+
ctx.deps.usage_limits,
767768
output_parts,
768769
deferred_calls,
769-
ctx.deps.usage_limits,
770770
):
771771
yield event
772772

@@ -811,9 +811,9 @@ async def _call_tools(
811811
tool_calls: list[_messages.ToolCallPart],
812812
deferred_tool_results: dict[str, DeferredToolResult],
813813
tracer: Tracer,
814+
usage_limits: _usage.UsageLimits | None,
814815
output_parts: list[_messages.ModelRequestPart],
815816
output_deferred_calls: dict[Literal['external', 'unapproved'], list[_messages.ToolCallPart]],
816-
usage_limits: _usage.UsageLimits | None,
817817
) -> AsyncIterator[_messages.HandleResponseEvent]:
818818
tool_parts_by_index: dict[int, _messages.ModelRequestPart] = {}
819819
user_parts_by_index: dict[int, _messages.UserPromptPart] = {}

pydantic_ai_slim/pydantic_ai/_tool_manager.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async def handle_call(
8686

8787
if (tool := self.tools.get(call.tool_name)) and tool.tool_def.kind == 'output':
8888
# Output tool calls are not traced
89-
return await self._call_tool(call, allow_partial, wrap_validation_errors, usage_limits, is_output_tool=True)
89+
return await self._call_tool(call, allow_partial, wrap_validation_errors, usage_limits)
9090
else:
9191
return await self._call_tool_traced(
9292
call,
@@ -103,7 +103,6 @@ async def _call_tool(
103103
allow_partial: bool,
104104
wrap_validation_errors: bool,
105105
usage_limits: UsageLimits | None,
106-
is_output_tool: bool = False,
107106
) -> Any:
108107
if self.tools is None or self.ctx is None:
109108
raise ValueError('ToolManager has not been prepared for a run step yet') # pragma: no cover
@@ -135,12 +134,12 @@ async def _call_tool(
135134
else:
136135
args_dict = validator.validate_python(call.args or {}, allow_partial=pyd_allow_partial)
137136

138-
if not is_output_tool and usage_limits is not None:
137+
if usage_limits is not None:
139138
usage_limits.check_before_tool_call(self.ctx.usage)
140139

141140
result = await self.toolset.call_tool(name, args_dict, ctx, tool)
142141

143-
if not is_output_tool:
142+
if usage_limits is not None:
144143
self.ctx.usage.tool_calls += 1
145144

146145
return result

0 commit comments

Comments
 (0)