Skip to content

fix(chat): Improve agent loop tracing#303

Merged
dcramer merged 7 commits into
getsentry:mainfrom
obostjancic:ognjenbostjancic/tet-2304-improve-juniors-instrumentation
May 21, 2026
Merged

fix(chat): Improve agent loop tracing#303
dcramer merged 7 commits into
getsentry:mainfrom
obostjancic:ognjenbostjancic/tet-2304-improve-juniors-instrumentation

Conversation

@obostjancic
Copy link
Copy Markdown
Member

@obostjancic obostjancic commented May 7, 2026

  • Add createTracedStreamFn — wraps streamSimple to emit a gen_ai.chat span per LLM iteration inside the pi-agent-core loop; captures input/output messages, token usage, finish reason, and response model; spans nest under the existing gen_ai.invoke_agent parent
  • Fix slackChannelListMessages: success path now returns a structured { content, details } result so gen_ai.tool.call.result is set in Sentry — previously the full messages array swamped the 12k attribute limit and the attribute only appeared on error returns

Before:
image

After:
CleanShot 2026-05-07 at 12 26 05

@vercel
Copy link
Copy Markdown

vercel Bot commented May 7, 2026

@obostjancic is attempting to deploy a commit to the Sentry Team on Vercel.

A member of the Team first needs to authorize it.

Comment thread packages/junior/src/chat/pi/traced-stream.ts Outdated
Comment thread packages/junior/src/chat/pi/traced-stream.ts
obostjancic and others added 2 commits May 21, 2026 12:42
Each iteration of the pi-agent-core agent loop now produces its own
gen_ai.chat Sentry span via a traced streamFn wrapper. This gives
visibility into individual LLM calls (input/output messages, token
usage, finish reasons) nested under the parent gen_ai.invoke_agent
span.

Co-Authored-By: Claude <[email protected]>
…s and fix traced-stream test key

slackChannelListMessages returned a plain object with the full messages
array as details, causing gen_ai.tool.call.result to exceed the 12k
attribute limit and only appear on error returns. Switch the success path
to a structured { content, details } result where details carries only
the compact metadata (ok, channel_id, count, next_cursor).

Also fix a stale key reference in the traced-stream test (turnId → runId).

Co-Authored-By: Claude <[email protected]>
@obostjancic obostjancic force-pushed the ognjenbostjancic/tet-2304-improve-juniors-instrumentation branch from a9661dd to faeadbb Compare May 21, 2026 11:13
Comment thread packages/junior/src/chat/respond.ts
- Set error status (code 2) on the gen_ai.chat span when base() throws
  or stream.result() rejects, so failed LLM calls appear as errors in
  Sentry rather than successful spans
- Wrap success callback body in try/finally so span.end() is guaranteed
  even if buildChatEndAttributes or setAttribute throws; suppress the
  resulting floating rejection with .catch()
- Pass createTracedStreamFn() into the advisor context so advisor agent
  loop iterations also produce gen_ai.chat child spans

Co-Authored-By: Claude <[email protected]>
Comment thread packages/junior/src/chat/tools/slack/channel-list-messages.ts
obostjancic and others added 3 commits May 21, 2026 13:33
…ents

- Add error status rule for failed gen_ai.chat spans to tracing spec
- Note traced streamFn now covers the advisor agent loop too
- Add gen_ai.chat to Agent And Model spans in TELEMETRY.md
- Add gen_ai.tool.call.result to Tools domain attributes

Co-Authored-By: Claude <[email protected]>
…ocal constant

Removes the duplicated "vercel-ai-gateway" string literal in traced-stream.ts
and reuses the canonical GEN_AI_PROVIDER_NAME export already used by all other
files in packages/junior/src/chat/pi/.

Co-Authored-By: Claude <[email protected]>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4cc8534. Configure here.

Comment thread packages/junior/src/chat/pi/traced-stream.ts
@obostjancic obostjancic requested a review from dcramer May 21, 2026 12:26
@dcramer dcramer merged commit 62045a1 into getsentry:main May 21, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants