Skip to content

feat(ai): capture $ai_span for LLM tool calls in provider wrappers - #799

Draft
marco-g-pm wants to merge 1 commit into
mainfrom
posthog-code/ai-tool-call-spans
Draft

feat(ai): capture $ai_span for LLM tool calls in provider wrappers#799
marco-g-pm wants to merge 1 commit into
mainfrom
posthog-code/ai-tool-call-spans

Conversation

@marco-g-pm

Copy link
Copy Markdown

💡 Motivation and Context

Raised from a PostHog Slack thread while comparing the OpenTelemetry integration with the @posthog/ai wrapper for direct OpenAI usage. A recurring question was where tool calls show up: the wrapper captured which tools the model chose only as data inside $ai_output_choices, never as spans, so tool calls didn't appear as nodes in the trace tree the way framework integrations (OpenAI Agents SDK, LangChain) render them.

This adds first-class tool-call spans to the direct-SDK wrappers so a tool call the LLM requests becomes a real $ai_span in the trace.

Why: so users on the OpenAI/Anthropic/Gemini wrappers get tool calls visualized as spans in the trace tree without adopting a full framework or OTel.

What changed

  • When a generation response contains tool calls, the wrapper now emits one child $ai_span per requested tool ($ai_span_type: "tool") with the tool name, arguments, and tool-call id, nested under the generation via $ai_parent_id.
  • Generation events now carry an $ai_span_id so the tool spans can parent to them.
  • Implemented once in the shared capture helpers, so it covers the sync, async, and streaming paths across OpenAI, Anthropic, and Gemini.

Scope / known limitation

The wrapper observes the model's response (the request to call a tool) but never executes the tool, so these spans have no duration or result — they mark which tools were chosen. Timing/results of the actual tool execution would need the execution instrumented directly (a follow-up with client.trace_tool(...)-style helper is the natural next step). Span capture is best-effort and is wrapped so it can never break the underlying LLM call.

💚 How did you test it?

Added/updated tests asserting the new $ai_span, its properties, and its parent linkage to the generation across OpenAI, Anthropic, and Gemini (sync, async, streaming). Ran the affected suites locally:

python -m pytest posthog/test/ai/openai posthog/test/ai/anthropic posthog/test/ai/gemini -k "not integration"
# 172 passed

Live-API integration tests and the LangChain callback tests were not run (they require provider API keys and are unaffected — LangChain uses a separate callback path).

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • Ran sampo add to generate a changeset file (added .sampo/changesets/ai-tool-call-spans.md)

🤖 Agent context

Autonomy: Human-driven (agent-assisted) — directed by @marco-g-pm from a Slack thread.

Authored by the PostHog Slack app (Claude Code). The thread weighed the OTel integration vs. the wrapper for direct SDKs; the agreed gap was that the wrapper captures tool choice as generation data but emits no spans. There was a design fork between (a) a passive span derived from the response and (b) an execution-timing helper the user wraps their tool call in. This PR implements (a) — it matches the "automatic capture on response" behavior requested and needs no call-site changes — and documents (b) as the follow-up for real duration/result. Implemented in the shared utils.py capture helpers so all three providers and all three call modes are covered from one place.


Created with PostHog from a Slack thread

When a generation response contains tool calls, the OpenAI/Anthropic/Gemini
wrappers now emit one child `$ai_span` (`$ai_span_type: "tool"`) per requested
tool — carrying the tool name, arguments, and tool-call id — nested under the
generation via `$ai_parent_id`. Generation events also now carry an
`$ai_span_id` so the spans can parent to them.

Covers the sync, async, and streaming capture paths (all converge on the shared
helpers in `utils.py`). The wrapper never executes the tool, so these spans have
no duration or result; span capture is best-effort and never breaks the LLM
call. Updated the OpenAI/Anthropic/Gemini tool-call tests to assert the new span
and its parent linkage.

Generated-By: PostHog Code
Task-Id: 150f5a08-87af-4dce-911e-770c06855fd1
@marco-g-pm marco-g-pm self-assigned this Jul 29, 2026
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-29 17:43:49 UTC
Duration: 338829ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 516ms
Endpoint And Method.Does Not Use Legacy Endpoints 1009ms
Required Headers.Has Authorization Bearer Header 1009ms
Required Headers.Has Content Type Json 1008ms
Required Headers.Has Posthog Sdk Info Format 1009ms
Required Headers.Has Posthog Attempt Header 1008ms
Required Headers.Has Posthog Request Id 1009ms
Required Headers.Has Posthog Request Timestamp 1009ms
Required Headers.Has User Agent 1008ms
Body Format.Body Has Created At And Batch 1009ms
Body Format.No Api Key In Body 1008ms
Body Format.No Sent At In Body 1009ms
Event Format.Event Has Required Root Fields 1009ms
Event Format.Event Uuid Is Valid 1007ms
Event Format.Event Timestamp Is Rfc3339 1009ms
Event Format.Distinct Id Is String 1008ms
Event Format.Distinct Id At Root Not Properties 1009ms
Event Format.Custom Properties Preserved 1008ms
Event Format.Set Properties Preserved 1008ms
Event Format.Set Once Properties Preserved 1008ms
Event Format.Groups Properties Preserved 1009ms
Event Format.Sdk Generates Uuid If Not Provided 1008ms
Event Format.Event Has Required Root Fields Batch 1011ms
Event Format.Event Uuid Is Valid Batch 1013ms
Event Format.Event Timestamp Is Rfc3339 Batch 1012ms
Event Format.Distinct Id Is String Batch 1012ms
Event Format.Distinct Id At Root Not Properties Batch 1012ms
Event Format.Custom Properties Preserved Batch 1012ms
Event Format.Set Properties Preserved Batch 1012ms
Event Format.Set Once Properties Preserved Batch 1011ms
Event Format.Groups Properties Preserved Batch 1013ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1012ms
Batch Behavior.Multiple Events In Single Batch 1506ms
Batch Behavior.Batch Envelope Smoke 1013ms
Batch Behavior.Flush With No Events Sends Nothing 1005ms
Batch Behavior.Flush At Triggers Batch 1509ms
Batch Behavior.Created At Reflects Batch Creation Time 1010ms
Deduplication.Generates Unique Uuids 1508ms
Deduplication.Different Events Same Content Different Uuids 1506ms
Deduplication.Preserves Uuid On Retry 7515ms
Deduplication.Preserves Timestamp On Retry 7511ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7508ms
Deduplication.No Duplicate Events In Batch 1507ms
Header Behavior On Retry.Attempt Header Starts At One 1008ms
Header Behavior On Retry.Attempt Header Increments On Retry 14523ms
Header Behavior On Retry.Request Id Preserved On Retry 7503ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3518ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7514ms
Response Format Validation.Success Response Has Uuid Keyed Results 1005ms
Response Format Validation.Success Response Has Ok For Each Event 1507ms
Response Format Validation.Success No Retry After When All Ok 1506ms
Response Format Validation.Success Retry After Present When Retry Events 2509ms
Response Format Validation.Success No Retry After When Drop Only 1508ms
Response Format Validation.Response Echoes Request Id 1008ms
Retry Behavior.Retries On 408 7512ms
Retry Behavior.Retries On 500 7510ms
Retry Behavior.Retries On 503 9514ms
Retry Behavior.Retries On 504 7515ms
Retry Behavior.Retryable Errors Have Retry After 4509ms
Retry Behavior.Respects Retry After On Retryable Error 12510ms
Retry Behavior.Does Not Retry On 400 3510ms
Retry Behavior.Does Not Retry On 401 3507ms
Retry Behavior.Does Not Retry On 402 3506ms
Retry Behavior.Does Not Retry On 413 3507ms
Retry Behavior.Does Not Retry On 415 3506ms
Retry Behavior.Non Retryable Errors Have No Retry After 3510ms
Retry Behavior.Implements Backoff 23530ms
Retry Behavior.Max Retries Respected 23521ms
Partial Batch Handling.Handles 200 Full Success 3000ms
Partial Batch Handling.Handles 200 With All Ok 4508ms
Partial Batch Handling.Does Not Retry Dropped Events 4506ms
Partial Batch Handling.Does Not Retry Limited Events 4510ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7513ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7511ms
Partial Batch Handling.Retries Only Retry Events From Partial 7511ms
Partial Batch Handling.Partial Retry Preserves Uuids 7507ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7517ms
Partial Batch Handling.Partial Retry Request Id Preserved 7511ms
Partial Batch Handling.Respects Retry After On Partial 9511ms
Partial Batch Handling.Unknown Result Treated As Terminal 4504ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4509ms
Compression.Sends Gzip Content Encoding 1008ms
Compression.No Content Encoding When Disabled 1008ms
Compression.Compressed Body Is Decompressible 1009ms
Error Handling.Does Not Retry On Unknown 4Xx 3508ms
Event Options.Cookieless Mode Override 1007ms
Event Options.Disable Skew Correction Override 1009ms
Event Options.Process Person Profile Override 1008ms
Event Options.Product Tour Id Override 1009ms
Event Options.Unset Options Omitted 1008ms
Event Options.Options Override In Batch 1013ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1008ms
Geoip And Historical Migration.Historical Migration Set In Body 1008ms
Geoip And Historical Migration.Historical Migration Absent By Default 1008ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1007ms
Request Payload.Flags Request Uses V2 Query Param 1007ms
Request Payload.Flags Request Hits Flags Path Not Decide 1006ms
Request Payload.Flags Request Omits Authorization Header 1006ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1006ms
Request Payload.Groups Default To Empty Object 1006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1006ms
Request Payload.Disable Geoip Omitted Defaults To False 1007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1006ms
Request Lifecycle.No Flags Request On Init Alone 503ms
Request Lifecycle.No Flags Request On Normal Capture 1506ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1010ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1003ms
Retry Behavior.Retries Flags On 502 1006ms
Retry Behavior.Retries Flags On 504 1006ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1509ms

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.

1 participant