You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contents/docs/ai-engineering/_snippets/anthropic.mdx
+55-4Lines changed: 55 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,22 @@
1
-
Start by installing the Anthropic Python SDK:
1
+
Start by installing the Anthropic SDK:
2
2
3
-
```bash
3
+
<MultiLanguage>
4
+
5
+
```bash file=Python
4
6
pip install anthropic
5
7
```
6
8
9
+
```bash file=TypeScript
10
+
npm install @anthropic-ai/sdk
11
+
```
12
+
13
+
</MultiLanguage>
14
+
7
15
In the spot where you initialize the Anthropic SDK, import PostHog and our Anthropic wrapper, initialize PostHog with your project API key and host (from [your project settings](https://us.posthog.com/settings/project)), and pass it to our Anthropic wrapper.
8
16
9
-
```python
17
+
<MultiLanguage>
18
+
19
+
```python
10
20
from posthog.ai.anthropic import Anthropic
11
21
import posthog
12
22
@@ -19,11 +29,30 @@ client = Anthropic(
19
29
)
20
30
```
21
31
32
+
```typescript
33
+
import { Anthropic } from'@posthog/ai'
34
+
import { PostHog } from'posthog-node'
35
+
36
+
const phClient =newPostHog(
37
+
'<ph_project_api_key>',
38
+
{ host: '<ph_client_api_host>' }
39
+
)
40
+
41
+
const client =newAnthropic({
42
+
apiKey: 'sk-ant-api...', // Replace with your Anthropic API key
43
+
posthog: phClient
44
+
})
45
+
```
46
+
47
+
</MultiLanguage>
48
+
22
49
> **Note:** This also works with the `AsyncAnthropic` client as well as `AnthropicBedrock`, `AnthropicVertex`, and the async versions of those.
23
50
24
51
Now, when you use the Anthropic SDK, it automatically captures many properties into PostHog including `$ai_input`, `$ai_input_tokens`, `$ai_latency`, `$ai_model`, `$ai_model_parameters`, `$ai_output_choices`, and `$ai_output_tokens`.
25
52
26
-
You can also capture additional properties like `posthog_distinct_id`, `posthog_trace_id`, `posthog_properties`, `posthog_groups`, and `posthog_privacy_mode`.
53
+
You can also capture or modify additional properties with the distinct ID, trace ID, properties, groups, and privacy mode parameters.
> - This also works when message streams are used (e.g. `stream=True` or `client.messages.stream(...)`).
49
100
> - If you want to capture LLM events anonymously, **don't** pass a distinct ID to the request. See our docs on [anonymous vs identified events](/docs/data/anonymous-vs-identified-events) to learn more.
0 commit comments