Skip to content

Commit bd5a255

Browse files
committed
feat: Add support for claude agent sdk hooks
1 parent cff3153 commit bd5a255

16 files changed

Lines changed: 1633 additions & 590 deletions

py/src/braintrust/wrappers/claude_agent_sdk/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from braintrust.logger import NOOP_SPAN, current_span, init_logger
2121

22-
from ._wrapper import _create_client_wrapper_class, _create_tool_wrapper_class, _wrap_tool_factory
22+
from ._wrapper import _create_client_wrapper_class, _create_tool_wrapper_class, _wrap_module_query, _wrap_tool_factory
2323

2424
logger = logging.getLogger(__name__)
2525

@@ -69,6 +69,7 @@ def setup_claude_agent_sdk(
6969
original_client = claude_agent_sdk.ClaudeSDKClient if hasattr(claude_agent_sdk, "ClaudeSDKClient") else None
7070
original_tool_class = claude_agent_sdk.SdkMcpTool if hasattr(claude_agent_sdk, "SdkMcpTool") else None
7171
original_tool_fn = claude_agent_sdk.tool if hasattr(claude_agent_sdk, "tool") else None
72+
original_query_fn = claude_agent_sdk.query if hasattr(claude_agent_sdk, "query") else None
7273

7374
if original_client:
7475
wrapped_client = _create_client_wrapper_class(original_client)
@@ -97,6 +98,15 @@ def setup_claude_agent_sdk(
9798
if getattr(module, "tool", None) is original_tool_fn:
9899
setattr(module, "tool", wrapped_tool_fn)
99100

101+
if original_query_fn:
102+
wrapped_query_fn = _wrap_module_query(original_query_fn)
103+
claude_agent_sdk.query = wrapped_query_fn
104+
105+
for module in list(sys.modules.values()):
106+
if module and hasattr(module, "query"):
107+
if getattr(module, "query", None) is original_query_fn:
108+
setattr(module, "query", wrapped_query_fn)
109+
100110
return True
101111
except ImportError:
102112
# Not installed - this is expected when using auto_instrument()

0 commit comments

Comments
 (0)