Skip to content

Commit 3810e7a

Browse files
authored
Docs: Anthropic TypeScript observability (#10584)
1 parent 799870b commit 3810e7a

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

contents/docs/ai-engineering/_snippets/anthropic.mdx

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
Start by installing the Anthropic Python SDK:
1+
Start by installing the Anthropic SDK:
22

3-
```bash
3+
<MultiLanguage>
4+
5+
```bash file=Python
46
pip install anthropic
57
```
68

9+
```bash file=TypeScript
10+
npm install @anthropic-ai/sdk
11+
```
12+
13+
</MultiLanguage>
14+
715
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.
816

9-
```python
17+
<MultiLanguage>
18+
19+
```python
1020
from posthog.ai.anthropic import Anthropic
1121
import posthog
1222

@@ -19,11 +29,30 @@ client = Anthropic(
1929
)
2030
```
2131

32+
```typescript
33+
import { Anthropic } from '@posthog/ai'
34+
import { PostHog } from 'posthog-node'
35+
36+
const phClient = new PostHog(
37+
'<ph_project_api_key>',
38+
{ host: '<ph_client_api_host>' }
39+
)
40+
41+
const client = new Anthropic({
42+
apiKey: 'sk-ant-api...', // Replace with your Anthropic API key
43+
posthog: phClient
44+
})
45+
```
46+
47+
</MultiLanguage>
48+
2249
> **Note:** This also works with the `AsyncAnthropic` client as well as `AnthropicBedrock`, `AnthropicVertex`, and the async versions of those.
2350
2451
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`.
2552

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.
54+
55+
<MultiLanguage>
2756

2857
```python
2958
response = client.messages.create(
@@ -44,6 +73,28 @@ response = client.messages.create(
4473
print(response.content[0].text)
4574
```
4675

76+
```typescript
77+
const response = await client.messages.create({
78+
model: "claude-3-5-sonnet-latest",
79+
messages: [
80+
{
81+
role: "user",
82+
content: "Tell me a fun fact about hedgehogs"
83+
}
84+
],
85+
posthogDistinctId: "user_123", // optional
86+
posthogTraceId: "trace_123", // optional
87+
posthogProperties: { conversationId: "abc123", paid: true }, // optional
88+
posthogGroups: { company: "company_id_in_your_db" }, // optional
89+
posthogPrivacyMode: false // optional
90+
})
91+
92+
console.log(response.content[0].text)
93+
phClient.shutdown()
94+
```
95+
96+
</MultiLanguage>
97+
4798
> **Notes:**
4899
> - This also works when message streams are used (e.g. `stream=True` or `client.messages.stream(...)`).
49100
> - 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

Comments
 (0)