Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions posthog/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid
from typing import Any, Callable, Dict, List, Optional, cast

from posthog import get_tags, identify_context, new_context, tag
from posthog import get_tags, identify_context, new_context, tag, contexts
from posthog.ai.sanitization import (
sanitize_anthropic,
sanitize_gemini,
Expand Down Expand Up @@ -366,6 +366,10 @@ def call_llm_and_track_usage(
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())

if not contexts.get_context_distinct_id():
# Use trace_id as district_id if it's not defined.
identify_context(posthog_trace_id)

if response and (
hasattr(response, "usage")
or (provider == "gemini" and hasattr(response, "usage_metadata"))
Expand Down Expand Up @@ -445,7 +449,7 @@ def call_llm_and_track_usage(
sdk_tags, posthog_properties
)
ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=contexts.get_context_distinct_id(),
event="$ai_generation",
properties=merged_properties,
groups=posthog_groups,
Expand Down Expand Up @@ -501,6 +505,10 @@ async def call_llm_and_track_usage_async(
if posthog_trace_id is None:
posthog_trace_id = str(uuid.uuid4())

if not contexts.get_context_distinct_id():
# Use trace_id as district_id if it's not defined.
identify_context(posthog_trace_id)

if response and (
hasattr(response, "usage")
or (provider == "gemini" and hasattr(response, "usage_metadata"))
Expand Down Expand Up @@ -580,7 +588,7 @@ async def call_llm_and_track_usage_async(
sdk_tags, posthog_properties
)
ph_client.capture(
distinct_id=posthog_distinct_id or posthog_trace_id,
distinct_id=contexts.get_context_distinct_id(),
event="$ai_generation",
properties=merged_properties,
groups=posthog_groups,
Expand Down