Skip to content

fix(ai): label tool errors in privacy mode and redact tool span content when sent - #1283

Open
DanielVisca wants to merge 4 commits into
mainfrom
posthog/ai-tool-span-results
Open

DanielVisca wants to merge 4 commits into
mainfrom
posthog/ai-tool-span-results

Conversation

@DanielVisca

@DanielVisca DanielVisca commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Follow-up to #1282, which shipped tool outcomes on Claude Agent SDK and LangChain tool spans. This PR started as a parallel version of that change. It is now rebuilt on main and keeps only what #1282 doesn't have, mostly fixes from review on this PR:

  • $ai_error for tool errors comes from one helper, _format_tool_error in utils.py:
    • In privacy mode it is the fixed label ToolError instead of being left out, so the span still counts in the errors view without sending tool output. On main the Claude Agent SDK omits it and LangChain sends None.
    • Outside privacy mode, media in the error content is redacted like $ai_output_state. On main, a LangChain error ToolMessage with a data URL or base64 block sends the raw media in $ai_error.
    • Empty content (None, "", []) gives the ToolError label instead of an empty string. Plain string items in LangChain list content are kept.
  • Claude Agent SDK tool input is redacted when the span is sent, not when the tool starts. If full media capture is turned off while a tool runs, the input is redacted with the setting in force when it's sent. If privacy mode is turned on, no input or output is sent.
  • Closing the public query() stream early now closes the processor's stream too, so tool spans still waiting for a result are sent. Before, only processor.query() did this.
  • PostHogMiddleware tool spans set $ai_span_type: "tool", like CallbackHandler tool spans.

💚 How did you test it?

  • uv run pytest posthog/test/ai: 1954 passed, 15 skipped.
  • Against main's source, the new and changed tests fail: privacy-mode ToolError (Claude Agent SDK and LangChain), input redaction after full capture turns off mid-tool, early close through the module query(), and the middleware span type. The _format_tool_error tests (media blocks, data URL, empty content, plain strings) can't import on main.
  • ruff format, ruff check, make public_api_check. The public API is unchanged.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Written by Claude Opus 5.5 in Claude Code, directed by Daniel. The branch was replaced by a single commit on main after #1282 merged, so the earlier review threads point at code that has since moved.

🤖 Generated with Claude Code

@DanielVisca
DanielVisca requested a review from a team as a code owner November 20, 2026 03:05
@DanielVisca DanielVisca self-assigned this Nov 20, 2026
@veria-ai

veria-ai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

Reviews

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed f26dc95: 1 Must fix, 3 Should fix.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

Comment thread posthog/ai/utils.py Resolved
Comment thread posthog/ai/langchain/callbacks.py Resolved
Comment thread posthog/ai/langchain/callbacks.py Resolved
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks completed. No changes flagged for review.

Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.

View all results
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 133.67 KiB/op 133.67 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,345.53 count/op 1,345.53 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 85.21 µs/op 84.18 µs/op 1.2% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 37.65 MiB 37.78 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 40.1 MiB 40.22 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 40.1 MiB 40.22 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 85.29 µs/op 83.99 µs/op 1.5% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 40.7 MiB 40.7 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 185.75 KiB/op 185.75 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,337.52 count/op 1,337.52 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 116.59 µs/op 110.78 µs/op 5% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.39 MiB 41.38 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 47.18 MiB 47.17 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 47.18 MiB 47.17 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 109.62 µs/op 103.18 µs/op 5.9% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 51.63 MiB 51.48 MiB 0.3% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 138.74 KiB/op 138.74 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,390.44 count/op 1,390.43 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 99.8 µs/op 101.19 µs/op 1.4% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 37.71 MiB 37.7 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 37.89 MiB 37.86 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 37.89 MiB 37.86 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 95.85 µs/op 98.36 µs/op 2.6% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 40.7 MiB 40.7 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 190.92 KiB/op 190.92 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,382.36 count/op 1,382.36 count/op No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 121.28 µs/op 123.8 µs/op 2.1% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 41.15 MiB 41.13 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 41.61 MiB 41.58 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 41.21 MiB 41.18 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 112.76 µs/op 117.66 µs/op 4.3% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.56 MiB 41.53 MiB <0.1% less memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 64.51 µs/op 62.52 µs/op 3.1% less time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 65.31 µs/op 65.13 µs/op 0.3% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 60.75 µs/op 59.89 µs/op 1.4% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 61.97 µs/op 62.41 µs/op 0.7% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 225.43 KiB/op 225.43 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,279.42 count/op 2,279.42 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 82.57 µs/op 82.81 µs/op 0.3% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 82.57 µs/op 82.81 µs/op 0.3% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,413.69 count/op 22,413.69 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 780.41 µs/op 774.59 µs/op 0.7% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 780.42 µs/op 774.66 µs/op 0.7% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.63 MiB/op 21.63 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 223,655.44 count/op 223,655.44 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 7.73 ms/op 7.66 ms/op 0.9% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 7.73 ms/op 7.66 ms/op 0.9% less time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 44.04 MiB 43.95 MiB 0.2% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 44.38 MiB 44.35 MiB <0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 44.38 MiB 44.35 MiB <0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 44.14 MiB 44.06 MiB 0.2% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 43.12 MiB 43.16 MiB <0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 43.12 MiB 43.16 MiB <0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 43.11 MiB 43.16 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 43.31 MiB 43.35 MiB <0.1% more memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 15.74 KiB/op 15.74 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 162.77 count/op 162.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 6.83 µs/op 6.75 µs/op 1.1% less time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 6.83 µs/op 6.75 µs/op 1.1% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 24.6 KiB/op 24.6 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 239.28 count/op 239.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 9.27 µs/op 9.27 µs/op <0.1% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 9.27 µs/op 9.27 µs/op <0.1% less time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 39.81 KiB/op 39.81 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 388.27 count/op 388.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 13.64 µs/op 13.54 µs/op 0.8% less time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 13.64 µs/op 13.54 µs/op 0.8% less time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 60.59 KiB/op 60.59 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 584.27 count/op 584.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 19.44 µs/op 19.61 µs/op 0.9% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 19.44 µs/op 19.61 µs/op 0.9% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 59.22 KiB/op 59.22 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 567.77 count/op 567.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 19.01 µs/op 18.6 µs/op 2.2% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 19.01 µs/op 18.6 µs/op 2.2% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.04 KiB/op 19.04 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 192.27 count/op 192.27 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 7.52 µs/op 7.64 µs/op 1.5% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 7.52 µs/op 7.64 µs/op 1.5% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 20.66 KiB/op 20.66 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 197.77 count/op 197.77 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 8.25 µs/op 8.32 µs/op 0.8% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 8.25 µs/op 8.32 µs/op 0.8% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 14.89 KiB/op 14.89 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 152.77 count/op 152.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 6.37 µs/op 6.35 µs/op 0.3% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 6.37 µs/op 6.35 µs/op 0.3% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 15.9 KiB/op 15.9 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 166.77 count/op 166.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 6.95 µs/op 6.93 µs/op 0.2% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 6.95 µs/op 6.93 µs/op 0.2% less time Not flagged

Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals.

Each shard uses its own runner; compare Before PR against With PR within a row, not across shards.

Compared 521db27011ed037482e1dcf5151611746783575f → d357b906c611192cc00cf5feff36643fb7367900.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

Comment thread posthog/ai/langchain/callbacks.py Resolved
@DanielVisca DanielVisca added the stamphog Request stamphog review label Nov 20, 2026

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not approved — this change needs a human reviewer.

Re-adding the stamphog label gives the same result unless the changed files change.

stamphog can't auto-review this pull request. The deny-list gate refused it because it touches references/public_api_snapshot.txt, which matches the public_api category. Because of that match, the tier gate placed the whole PR in the never-auto-approve tier (T2-never). The size gate passed, so the refusal isn't about size.

The author's note already asks the AI observability team to review and own it, so the next step is to get that human review. Any change to the public API snapshot needs a person to confirm it was intended.

Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✗ matches: public_api in references/public_api_snapshot.txt
every_file_attested ✓ not a bot PR
size ✓ 395L, 5F substantive, 746L/11F incl. docs/generated/snapshots — within ceiling
tier ✗ T2-never because the deny-list matched public_api
stamphog 2.9.1 .stamphog/policy.yml @ unknown · reviewed head 8f4da4b

@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 20, 2026
@DanielVisca DanielVisca added the stamphog Request stamphog review label Nov 20, 2026
@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 20, 2026
@stamphog

stamphog Bot commented Nov 20, 2026

Copy link
Copy Markdown

No review started. stamphog started a review of this pull request less than 10 minutes ago, and a label can request at most one review per 10 minutes. The stamphog label has been removed. Add it again at 03:48 UTC (in about 4 minutes) to request a fresh review.

@DanielVisca DanielVisca added the stamphog Request stamphog review label Nov 20, 2026
stamphog[bot]

This comment was marked as outdated.

@DanielVisca
DanielVisca requested a review from a team November 23, 2026 20:29

@dustinbyrne dustinbyrne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI-assisted review.

The Claude Agent SDK deferred tool-span tracking (_ToolSpanTracker, subagent matching, aclose()/ResultMessage/disconnect() flushing), LangChain ToolMessage dict metadata preservation, and privacy/media redaction work well — including using the fixed "ToolError" label in privacy mode, which normalizes cleanly in PostHog error ingestion (processAiErrorNormalization) without leaking tool output.

Verified end-to-end delivery over real HTTP transport (/i/v1/ai/events) with sdk-mock-server for both Claude Agent SDK and LangChain CallbackHandler, plus base-vs-head regression checks. Leaving a few inline items before approval — most notably a regression where non-tool LangChain chains and root $ai_trace events returning an error ToolMessage are also marked $ai_is_error: True, a scope gap with PostHogMiddleware vs the changeset, and edge-case handling in _format_tool_error.

Follow-up (non-blocking, out of PR scope):

  • PostHog/posthog-js (@posthog/ai): For cross-SDK parity, @posthog/ai still omits $ai_error on Claude Agent SDK tool spans (packages/ai/src/claude-agent-sdk/processor.ts), and lacks $ai_span_type: 'tool', error-ToolMessage detection, and tool_call_id/name/status preservation in LangChainCallbackHandler (packages/ai/src/langchain/callbacks.ts), plus $ai_span_type: 'tool' in createPostHogMiddleware (packages/ai/src/langchain/middleware/index.ts).
  • Untouched posthog-python files: posthog/ai/claude_agent_sdk/formatting.py currently omits id on ToolUseBlock in $ai_output_choices and is_error on ToolResultBlock in $ai_input (both are emitted by @posthog/ai in formatting.ts), and PostHogMiddleware._returned_tool_error (posthog/ai/langchain/middleware.py:328-331) still uses RuntimeError(str(response.content)) without finalize_ai_content media redaction and emits "RuntimeError: Tool returned an error" in privacy mode rather than "ToolError". Consider aligning these in follow-up PRs with equivalent regression tests.

Comment thread posthog/ai/langchain/callbacks.py Resolved
Comment thread posthog/ai/utils.py Resolved
@dustinbyrne
dustinbyrne requested a review from a team November 23, 2026 23:53
@stamphog
stamphog Bot dismissed their stale review November 23, 2026 23:57

A new stamphog review started for this PR — the fresh verdict replaces this approval.

@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 23, 2026

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not approved — escalated to a human reviewer.

Add the stamphog label to request another review once you have addressed this.

The current code addresses all four inline findings, but the risky telemetry and privacy changes have no qualifying current-head assurance. Request a review from an AI observability teammate before re-requesting; Andrew Maguire or Eli Reisman are suggested reviewers based on familiarity.

  • Risky territory without independent assurance: Data ingestion or write paths — posthog/ai/claude_agent_sdk/processor.py and client.py defer telemetry emission until tool results arrive and change the result, error and latency properties sent to PostHog.; Data ingestion or write paths — posthog/ai/langchain/callbacks.py and middleware.py change tool-span classification, error reporting and tool-result metadata sent in AI events.; Auth or security-sensitive surface — posthog/ai/utils.py and claude_agent_sdk/processor.py govern privacy-mode suppression and media redaction for newly captured tool outputs and errors.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 406L, 6F substantive, 815L/12F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1d-complex (815L, 12F, two-areas, feat)
stamphog 2.9.1 .stamphog/policy.yml @ 62dba83 · reviewed head 62dba83

@DanielVisca

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@DanielVisca PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed 62dba83: no findings.

@DanielVisca

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@DanielVisca PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed 898feb0: 1 Should fix.

@DanielVisca

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@DanielVisca PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed f30cb7b: no findings.

@DanielVisca DanielVisca added the stamphog Request stamphog review label Nov 24, 2026
stamphog[bot]

This comment was marked as outdated.

…nt when sent

Follow-up to #1282, rebuilt on main:
- $ai_error comes from _format_tool_error: "ToolError" in privacy mode or for
  empty content, media redacted, plain string items kept
- Claude Agent SDK tool input is redacted when the span is sent, so settings
  changed while the tool ran apply
- the public query() closes the processor stream when the caller stops early,
  so pending tool spans are sent
- PostHogMiddleware tool spans set $ai_span_type "tool"

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DanielVisca
DanielVisca force-pushed the posthog/ai-tool-span-results branch from f30cb7b to bb8bc19 Compare November 24, 2026 18:38
@stamphog
stamphog Bot dismissed their stale review November 24, 2026 18:38

A new stamphog review started for this PR — the fresh verdict replaces this approval.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stamphog[bot]

This comment was marked as outdated.

@DanielVisca DanielVisca changed the title feat(ai): send tool results and errors on tool spans fix(ai): label tool errors in privacy mode and redact tool span content when sent Nov 24, 2026
@DanielVisca

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@DanielVisca PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed b5e57f9: 1 Should fix.

This is review round 3 on this pull request. When findings keep coming back in the same code, the approach is usually the problem. Before the next fix, check whether a different design would remove the whole class of findings.

Tool spans again remove $ai_input_state in privacy mode, including one
supplied through posthog_properties.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DanielVisca

Copy link
Copy Markdown
Contributor Author

@PostHog review

@stamphog
stamphog Bot dismissed their stale review November 24, 2026 18:46

A new stamphog review started for this PR — the fresh verdict replaces this approval.

@posthog

posthog Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@DanielVisca PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed af53274: no findings.

stamphog[bot]

This comment was marked as outdated.

@Radu-Raicea
Radu-Raicea requested review from carlos-marchal-ph and removed request for a team November 25, 2026 16:59
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@stamphog
stamphog Bot dismissed their stale review November 25, 2026 17:50

A new stamphog review started for this PR — the fresh verdict replaces this approval.

@DanielVisca

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@DanielVisca PostHog Review is reviewing this pull request.

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approved.

Privacy-sensitive telemetry changes have targeted regression tests and STRONG author familiarity, satisfying the policy's assurance path despite no current-head reviews. The earlier actionable review concerns are addressed in the current code; explicitly deferred middleware behavior remains outside this change.

  • Author wrote 83% of the modified lines and has 4 merged PRs in these paths (familiarity STRONG).
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 127L, 5F substantive, 278L/10F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1c-medium (278L, 10F, two-areas, fix)
stamphog 2.9.1 .stamphog/policy.yml @ d357b90 · reviewed head d357b90

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed d357b90: no findings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stamphog Request stamphog review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants