|
| 1 | +--- |
| 2 | +title: "PostHog Integration" |
| 3 | +sidebarTitle: "PostHog" |
| 4 | +description: "Integrate Helicone AI Gateway with PostHog to automatically export LLM request events to your PostHog analytics platform for unified product analytics." |
| 5 | +"twitter:title": "PostHog Integration - Helicone OSS LLM Observability" |
| 6 | +iconType: "solid" |
| 7 | +--- |
| 8 | + |
| 9 | +import { strings } from "/snippets/strings.mdx"; |
| 10 | +import Star from "/snippets/star.mdx"; |
| 11 | +import RequestIntegration from "/snippets/request-integration.mdx"; |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +[PostHog](https://www.posthog.com/) is a comprehensive product analytics platform that helps you understand user behavior and product performance. |
| 16 | + |
| 17 | +## {strings.howToIntegrate} |
| 18 | + |
| 19 | +<Steps> |
| 20 | + <Step title={strings.generateKey}> |
| 21 | + <p>Sign up at <a href="https://www.helicone.ai" target="_blank">helicone.ai</a> and generate an <a href="https://us.helicone.ai/settings/api-keys" target="_blank">API key</a>.</p> |
| 22 | + <p>Create a <a href="https://posthog.com" target="_blank">Posthog account</a> if you don't have one. Get your Project API Key from your <a href="https://us.posthog.com/settings/project" target="_blank">PostHog project settings</a>.</p> |
| 23 | + |
| 24 | + ```env |
| 25 | + HELICONE_API_KEY=sk-helicone-... |
| 26 | + POSTHOG_PROJECT_API_KEY=phc_... |
| 27 | +
|
| 28 | + # Optional: PostHog host (defaults to https://app.posthog.com) |
| 29 | + # Only needed if using self-hosted PostHog |
| 30 | + # POSTHOG_CLIENT_API_HOST=https://app.posthog.com |
| 31 | + ``` |
| 32 | + </Step> |
| 33 | + |
| 34 | + <Step title={strings.installSDK('OpenAI')}> |
| 35 | + <CodeGroup> |
| 36 | + ```bash TypeScript |
| 37 | + npm install openai |
| 38 | + # or |
| 39 | + yarn add openai |
| 40 | + ``` |
| 41 | + |
| 42 | + ```bash Python |
| 43 | + pip install openai |
| 44 | + ``` |
| 45 | + </CodeGroup> |
| 46 | + </Step> |
| 47 | + |
| 48 | + <Step title="Configure OpenAI client with Helicone AI Gateway"> |
| 49 | + <CodeGroup> |
| 50 | + ```typescript TypeScript |
| 51 | + import { OpenAI } from "openai"; |
| 52 | + import dotenv from "dotenv"; |
| 53 | + |
| 54 | + dotenv.config(); |
| 55 | + |
| 56 | + const client = new OpenAI({ |
| 57 | + baseURL: "https://ai-gateway.helicone.ai", |
| 58 | + apiKey: process.env.HELICONE_API_KEY, |
| 59 | + defaultHeaders: { |
| 60 | + "Helicone-Posthog-Key": POSTHOG_PROJECT_API_KEY, |
| 61 | + "Helicone-Posthog-Host": POSTHOG_CLIENT_API_HOST |
| 62 | + }, |
| 63 | + }); |
| 64 | + ``` |
| 65 | + |
| 66 | + ```python Python |
| 67 | + import os |
| 68 | + from openai import OpenAI |
| 69 | + from dotenv import load_dotenv |
| 70 | + |
| 71 | + load_dotenv() |
| 72 | + |
| 73 | + client = OpenAI( |
| 74 | + base_url="https://ai-gateway.helicone.ai", |
| 75 | + api_key=os.getenv("HELICONE_API_KEY"), |
| 76 | + default_headers={ |
| 77 | + "Helicone-Posthog-Key": os.getenv("POSTHOG_PROJECT_API_KEY"), |
| 78 | + "Helicone-Posthog-Host": os.getenv("POSTHOG_CLIENT_API_HOST") |
| 79 | + }, |
| 80 | + ) |
| 81 | + ``` |
| 82 | + </CodeGroup> |
| 83 | + |
| 84 | + <div dangerouslySetInnerHTML={{ __html: strings.modelRegistryDescription }} /> |
| 85 | + </Step> |
| 86 | + |
| 87 | + <Step title={strings.useTheSDK('OpenAI')}> |
| 88 | + Your existing OpenAI code continues to work without any changes. Events will automatically be exported to PostHog. |
| 89 | + |
| 90 | + <CodeGroup> |
| 91 | + ```typescript TypeScript |
| 92 | + const response = await client.chat.completions.create({ |
| 93 | + model: "gpt-4o-mini", |
| 94 | + messages: [{ role: "user", content: "Hello, world!" }], |
| 95 | + temperature: 0.7, |
| 96 | + }); |
| 97 | + |
| 98 | + console.log(response.choices[0]?.message?.content); |
| 99 | + ``` |
| 100 | + |
| 101 | + ```python Python |
| 102 | + response = client.chat.completions.create( |
| 103 | + model="gpt-4o-mini", |
| 104 | + messages=[{"role": "user", "content": "Hello, world!"}], |
| 105 | + temperature=0.7, |
| 106 | + ) |
| 107 | + |
| 108 | + print("Completion:", response.choices[0].message.content) |
| 109 | + ``` |
| 110 | + </CodeGroup> |
| 111 | + </Step> |
| 112 | + |
| 113 | + <Step title={strings.verifyInHelicone}> |
| 114 | + <div dangerouslySetInnerHTML={{ __html: strings.verifyInHeliconeDesciption("Posthog") }} /> |
| 115 | + |
| 116 | + 1. Go to your <a href="https://us.posthog.com/events" target="_blank">PostHog Events</a> page |
| 117 | + 2. Look for events with the <code>helicone_request</code> event name |
| 118 | + 3. Each event contains metadata about the LLM request including: |
| 119 | + - Model used |
| 120 | + - Token counts |
| 121 | + - Latency |
| 122 | + - Cost |
| 123 | + - Request/response data |
| 124 | + </Step> |
| 125 | +</Steps> |
| 126 | + |
| 127 | +<Star /> |
| 128 | +<RequestIntegration /> |
| 129 | + |
| 130 | +## Related Documentation |
| 131 | + |
| 132 | +<CardGroup cols={2}> |
| 133 | + <Card title="AI Gateway Overview" icon="arrow-progress" href="/gateway/overview"> |
| 134 | + Learn about Helicone's AI Gateway features and capabilities |
| 135 | + </Card> |
| 136 | + <Card title="Custom Properties" icon="tags" href="/features/advanced-usage/custom-properties"> |
| 137 | + Add metadata to track and filter your requests |
| 138 | + </Card> |
| 139 | + <Card title="Sessions" icon="link" href="/features/sessions"> |
| 140 | + Track multi-turn conversations and user sessions |
| 141 | + </Card> |
| 142 | + <Card title="Model Registry" icon="database" href="https://helicone.ai/models"> |
| 143 | + Browse all available models and providers |
| 144 | + </Card> |
| 145 | +</CardGroup> |
0 commit comments