Repository navigation
feat(server-utils): Emit execute_tool spans for Anthropic's beta.sessions.events.toolRunner()
#25554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(server-utils): Emit execute_tool spans for Anthropic's beta.sessions.events.toolRunner()
#25554
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
da32a71
feat(server-utils): Emit execute_tool spans for Anthropic's beta.sess…
RulaKhaled 3b5f21c
Merge branch 'develop' into feat/anthropic-sessions-tool-runner
RulaKhaled 9564843
ref(server-utils): Guard the session tool runner like the messages one
RulaKhaled File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
...s/node-integration-tests/suites/tracing/anthropic/v0.129/scenario-session-tool-runner.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import Anthropic from '@anthropic-ai/sdk'; | ||
| import { SessionToolRunner } from '@anthropic-ai/sdk/resources/beta/sessions/events'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
|
|
||
| const STREAM_EVENTS = { | ||
| sesn_weather: [ | ||
| { type: 'agent.tool_use', id: 'sevt_weather', name: 'weather', input: { city: 'Vienna' } }, | ||
| { type: 'agent.tool_use', id: 'sevt_explode', name: 'explode', input: {} }, | ||
| { type: 'session.status_terminated', id: 'sevt_weather_end' }, | ||
| ], | ||
| // Served by a runner built with `new SessionToolRunner()`, as `EnvironmentWorker` does. | ||
| sesn_direct: [ | ||
| { type: 'agent.tool_use', id: 'sevt_read', name: 'read', input: { file_path: 'README.md' } }, | ||
| { type: 'session.status_terminated', id: 'sevt_direct_end' }, | ||
| ], | ||
| }; | ||
|
|
||
| function startMockSessionsServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.get('/anthropic/v1/sessions/:sessionId/events/stream', (req, res) => { | ||
| res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' }); | ||
|
|
||
| const events = STREAM_EVENTS[req.params.sessionId]; | ||
| events.forEach((event, index) => { | ||
| setTimeout(() => { | ||
| res.write(`event: ${event.type}\n`); | ||
| res.write(`data: ${JSON.stringify(event)}\n\n`); | ||
| if (index === events.length - 1) { | ||
| res.end(); | ||
| } | ||
| }, index * 10); | ||
| }); | ||
| }); | ||
| app.get('/anthropic/v1/sessions/:sessionId/events', (_req, res) => { | ||
| res.json({ data: [], next_page: null }); | ||
| }); | ||
| app.post('/anthropic/v1/sessions/:sessionId/events', (_req, res) => { | ||
| res.json({ data: [] }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function drain(runner) { | ||
| for await (const _call of runner) { | ||
| // drain | ||
| } | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockSessionsServer(); | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const client = new Anthropic({ | ||
| apiKey: 'mock-api-key', | ||
| baseURL: `http://localhost:${server.address().port}/anthropic`, | ||
| }); | ||
|
|
||
| await drain( | ||
| client.beta.sessions.events.toolRunner('sesn_weather', { | ||
| tools: [ | ||
| { | ||
| name: 'weather', | ||
| description: 'Get the weather by city', | ||
| input_schema: { type: 'object', properties: { city: { type: 'string' } } }, | ||
| run: () => 'sunny', | ||
| }, | ||
| { | ||
| name: 'explode', | ||
| description: 'Always fails', | ||
| input_schema: { type: 'object', properties: {} }, | ||
| run: () => { | ||
| throw new Error('Tool exploded'); | ||
| }, | ||
| }, | ||
| ], | ||
| }), | ||
| ); | ||
|
|
||
| await drain( | ||
| new SessionToolRunner('sesn_direct', { | ||
| client, | ||
| tools: [ | ||
| { | ||
| name: 'read', | ||
| description: 'Read a file', | ||
| input_schema: { type: 'object', properties: { file_path: { type: 'string' } } }, | ||
| run: () => '# Weather app', | ||
| }, | ||
| ], | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
|
|
||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Should've been in my review, but I forgot to hit the button first 😅)
The constructor hook targets
EnvironmentWorker. But the worker drives each runner from its own poll loop (lib/environments/worker.mjs,for await (const _ of runner)), and nothing in user code wraps that loop in a span. So each tool call becomes its own root span (its own segment under span streaming). A busy session would produce lots of separateexecute_toolsegments, but not tied to the session, because theagent.tool_useevent has no session id, and the span sets nogen_ai.conversation.id, which may be noisy and hard to use.Suggestion: we could consider a parent span per runner iteration? For example, wrap the runner's
[Symbol.asyncIterator]in aninvoke_agent-style span that setsgen_ai.conversation.idfromrunner.sessionId, so the tool spans nest under it. Or if that's out of scope, add a line to the PR body that saysEnvironmentWorkertool calls show up as root spans, and open a follow-up issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me! fwiw anthropic's own otel tracing puts one anthropic.environment.work span around each session and nests the tool spans under it, so a per runner span sounds right. opened #25768 for it