Skip to content

Commit 9ed2220

Browse files
andrewm4894claude
andcommitted
fix: isolate LLM context with fresh=True to avoid tag inheritance
Use fresh=True to start with a clean context for each LLM call. This avoids inheriting $ai_* tags from parent contexts which could cause mismatched AI metadata due to the tag merge order bug in contexts.py (parent tags incorrectly override child tags). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent dc23143 commit 9ed2220

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

posthog/ai/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def call_llm_and_track_usage(
257257
usage: TokenUsage = TokenUsage()
258258
error_params: Dict[str, Any] = {}
259259

260-
with new_context(client=ph_client, capture_exceptions=False):
260+
with new_context(client=ph_client, capture_exceptions=False, fresh=True):
261261
if posthog_distinct_id:
262262
identify_context(posthog_distinct_id)
263263

@@ -379,7 +379,7 @@ async def call_llm_and_track_usage_async(
379379
usage: TokenUsage = TokenUsage()
380380
error_params: Dict[str, Any] = {}
381381

382-
with new_context(client=ph_client, capture_exceptions=False):
382+
with new_context(client=ph_client, capture_exceptions=False, fresh=True):
383383
if posthog_distinct_id:
384384
identify_context(posthog_distinct_id)
385385

0 commit comments

Comments
 (0)