Skip to content

Commit 8331af7

Browse files
authored
feat: cached tokens (#202)
* feat: cached tokens * feat: add tool support * chore: local test * chore: isort black * chore: bump v * chore: remove import * fix: types * fix: black * fix: mypy unpacking of None * chore: mypy baseline * feat: mypy fix * fix: did things and stuff * fix: mypy yourpy whos py? * fix: things can be None * fix: move test * fix remove exampels from package * fix: losing my py
1 parent f4c9971 commit 8331af7

File tree

12 files changed

+499
-236
lines changed

12 files changed

+499
-236
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.19.0 – 2025-03-04
2+
3+
1. Add support for tool calls in OpenAI and Anthropic.
4+
2. Add support for cached tokens.
5+
16
## 3.18.1 – 2025-03-03
27

38
1. Improve quota-limited feature flag logs

llm_observability_examples.py

Lines changed: 0 additions & 221 deletions
This file was deleted.

mypy-baseline.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,13 @@ sentry_django_example/sentry_django_example/settings.py:0: error: Need type anno
4343
sentry_django_example/sentry_django_example/settings.py:0: error: Incompatible types in assignment (expression has type "str", variable has type "None") [assignment]
4444
posthog/ai/openai/openai_async.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
4545
posthog/ai/openai/openai_async.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
46-
posthog/ai/openai/openai_async.py:0: error: Unpacked dict entry 11 has incompatible type "dict[str, Any] | None"; expected "SupportsKeysAndGetItem[str, Any]" [dict-item]
4746
posthog/ai/openai/openai_async.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
48-
posthog/ai/openai/openai_async.py:0: error: Unpacked dict entry 8 has incompatible type "dict[str, Any] | None"; expected "SupportsKeysAndGetItem[str, Any]" [dict-item]
4947
posthog/ai/openai/openai.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
5048
posthog/ai/openai/openai.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
51-
posthog/ai/openai/openai.py:0: error: Unpacked dict entry 11 has incompatible type "dict[str, Any] | None"; expected "SupportsKeysAndGetItem[str, Any]" [dict-item]
5249
posthog/ai/openai/openai.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
53-
posthog/ai/openai/openai.py:0: error: Unpacked dict entry 8 has incompatible type "dict[str, Any] | None"; expected "SupportsKeysAndGetItem[str, Any]" [dict-item]
5450
posthog/ai/anthropic/anthropic_async.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
5551
posthog/ai/anthropic/anthropic_async.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
5652
posthog/ai/anthropic/anthropic_async.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
5753
posthog/ai/anthropic/anthropic.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
5854
posthog/ai/anthropic/anthropic.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
5955
posthog/ai/anthropic/anthropic.py:0: error: Incompatible types in assignment (expression has type "UUID", variable has type "str | None") [assignment]
60-
llm_observability_examples.py:0: error: Argument "posthog_client" to "OpenAI" has incompatible type Module; expected "Client" [arg-type]
61-
llm_observability_examples.py:0: error: Argument "posthog_client" to "AsyncOpenAI" has incompatible type Module; expected "Client" [arg-type]

posthog/ai/anthropic/anthropic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def generator():
125125
for k in [
126126
"input_tokens",
127127
"output_tokens",
128+
"cache_read_input_tokens",
129+
"cache_creation_input_tokens",
128130
]
129131
}
130132

@@ -184,6 +186,8 @@ def _capture_streaming_event(
184186
"$ai_http_status": 200,
185187
"$ai_input_tokens": usage_stats.get("input_tokens", 0),
186188
"$ai_output_tokens": usage_stats.get("output_tokens", 0),
189+
"$ai_cache_read_input_tokens": usage_stats.get("cache_read_input_tokens", 0),
190+
"$ai_cache_creation_input_tokens": usage_stats.get("cache_creation_input_tokens", 0),
187191
"$ai_latency": latency,
188192
"$ai_trace_id": posthog_trace_id,
189193
"$ai_base_url": str(self._client.base_url),

posthog/ai/anthropic/anthropic_async.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ async def generator():
125125
for k in [
126126
"input_tokens",
127127
"output_tokens",
128+
"cache_read_input_tokens",
129+
"cache_creation_input_tokens",
128130
]
129131
}
130132

@@ -184,6 +186,8 @@ async def _capture_streaming_event(
184186
"$ai_http_status": 200,
185187
"$ai_input_tokens": usage_stats.get("input_tokens", 0),
186188
"$ai_output_tokens": usage_stats.get("output_tokens", 0),
189+
"$ai_cache_read_input_tokens": usage_stats.get("cache_read_input_tokens", 0),
190+
"$ai_cache_creation_input_tokens": usage_stats.get("cache_creation_input_tokens", 0),
187191
"$ai_latency": latency,
188192
"$ai_trace_id": posthog_trace_id,
189193
"$ai_base_url": str(self._client.base_url),

posthog/ai/langchain/callbacks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,11 @@ def _capture_generation(
516516
"$ai_base_url": run.base_url,
517517
}
518518
if run.tools:
519-
event_properties["$ai_tools"] = run.tools
519+
event_properties["$ai_tools"] = with_privacy_mode(
520+
self._client,
521+
self._privacy_mode,
522+
run.tools,
523+
)
520524

521525
if isinstance(output, BaseException):
522526
event_properties["$ai_http_status"] = _get_http_status(output)

0 commit comments

Comments
 (0)