Skip to content

Commit dfd9e4d

Browse files
authored
feat: trace tool calls in agents (#790)
1 parent 1b89285 commit dfd9e4d

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

packages/ragbits-agents/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## Unreleased
44
- Add tool_choice parameter to agent interface (#738)
55
- Add PydanticAI agents support (#755)
6+
- Add unique ID to each agent instance for better tracking and identification
7+
- Add audit tracing for tool calls to improve observability and debugging
68

79
## 1.2.2 (2025-08-08)
810

packages/ragbits-agents/src/ragbits/agents/_main.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import uuid
23
from collections.abc import AsyncGenerator, AsyncIterator, Callable
34
from contextlib import suppress
45
from copy import deepcopy
@@ -185,6 +186,7 @@ def __init__(
185186
default_options: The default options for the agent run.
186187
"""
187188
super().__init__(default_options)
189+
self.id = uuid.uuid4().hex[:8]
188190
self.llm = llm
189191
self.prompt = prompt
190192
self.tools = [Tool.from_callable(tool) for tool in tools or []]
@@ -493,9 +495,8 @@ async def _get_all_tools(self) -> dict[str, Tool]:
493495

494496
return tools_mapping
495497

496-
@staticmethod
497498
async def _execute_tool(
498-
tool_call: ToolCall, tools_mapping: dict[str, Tool], context: AgentRunContext | None = None
499+
self, tool_call: ToolCall, tools_mapping: dict[str, Tool], context: AgentRunContext | None = None
499500
) -> ToolCallResult:
500501
if tool_call.type != "function":
501502
raise AgentToolNotSupportedError(tool_call.type)
@@ -505,18 +506,28 @@ async def _execute_tool(
505506

506507
tool = tools_mapping[tool_call.name]
507508

508-
try:
509-
call_args = tool_call.arguments.copy()
510-
if tool.context_var_name:
511-
call_args[tool.context_var_name] = context
512-
513-
tool_output = (
514-
await tool.on_tool_call(**call_args)
515-
if iscoroutinefunction(tool.on_tool_call)
516-
else tool.on_tool_call(**call_args)
517-
)
518-
except Exception as e:
519-
raise AgentToolExecutionError(tool_call.name, e) from e
509+
with trace(agent_id=self.id, tool_name=tool_call.name, tool_arguments=tool_call.arguments) as outputs:
510+
try:
511+
call_args = tool_call.arguments.copy()
512+
if tool.context_var_name:
513+
call_args[tool.context_var_name] = context
514+
515+
tool_output = (
516+
await tool.on_tool_call(**call_args)
517+
if iscoroutinefunction(tool.on_tool_call)
518+
else tool.on_tool_call(**call_args)
519+
)
520+
521+
outputs.result = {
522+
"tool_output": tool_output,
523+
"tool_call_id": tool_call.id,
524+
}
525+
except Exception as e:
526+
outputs.result = {
527+
"error": str(e),
528+
"tool_call_id": tool_call.id,
529+
}
530+
raise AgentToolExecutionError(tool_call.name, e) from e
520531

521532
return ToolCallResult(
522533
id=tool_call.id,

0 commit comments

Comments
 (0)