Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/example-ai-cloudflare-ai-gateway/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
POSTHOG_API_KEY=phc_your_project_api_key
POSTHOG_HOST=https://us.i.posthog.com
OPENAI_API_KEY=your_openai_api_key
CF_AIG_TOKEN=your_cloudflare_ai_gateway_token
CF_AIG_ACCOUNT_ID=your_cloudflare_account_id
CF_AIG_GATEWAY_ID=your_gateway_id
25 changes: 25 additions & 0 deletions examples/example-ai-cloudflare-ai-gateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Cloudflare AI Gateway + PostHog AI Examples

Track Cloudflare AI Gateway API calls with PostHog via the OpenAI-compatible unified endpoint.

## Setup

```bash
cp .env.example .env
# Fill in your API keys in .env
# Install uv if you haven't already: https://docs.astral.sh/uv/getting-started/installation/
uv sync
```

`POSTHOG_API_KEY`, `OPENAI_API_KEY`, `CF_AIG_TOKEN`, `CF_AIG_ACCOUNT_ID`, and `CF_AIG_GATEWAY_ID` are required. `CF_AIG_TOKEN` is your Cloudflare AI Gateway API token, passed via the `cf-aig-authorization` header.

## Examples

- **chat.py** - Chat completions via Cloudflare AI Gateway

## Run

```bash
source .env
uv run python chat.py
```
47 changes: 47 additions & 0 deletions examples/example-ai-cloudflare-ai-gateway/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Cloudflare AI Gateway chat completions via OpenAI-compatible API, tracked by PostHog via OpenTelemetry."""

import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from posthog.ai.otel import PostHogSpanProcessor
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor

resource = Resource(
attributes={
SERVICE_NAME: "example-cloudflare-ai-gateway-app",
"posthog.distinct_id": "example-user",
"foo": "bar",
"conversation_id": "abc-123",
}
)
provider = TracerProvider(resource=resource)
provider.add_span_processor(
PostHogSpanProcessor(
api_key=os.environ["POSTHOG_API_KEY"],
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
)
)
trace.set_tracer_provider(provider)

OpenAIInstrumentor().instrument()

import openai # noqa: E402

client = openai.OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
default_headers={
"cf-aig-authorization": f"Bearer {os.environ['CF_AIG_TOKEN']}",
},
base_url=f"https://gateway.ai.cloudflare.com/v1/{os.environ['CF_AIG_ACCOUNT_ID']}/{os.environ['CF_AIG_GATEWAY_ID']}/compat",
)

response = client.chat.completions.create(
model="openai/gpt-5-mini",
max_completion_tokens=1024,
messages=[
{"role": "user", "content": "Tell me a fun fact about hedgehogs."},
],
)

print(response.choices[0].message.content)
13 changes: 13 additions & 0 deletions examples/example-ai-cloudflare-ai-gateway/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "example-ai-cloudflare-ai-gateway"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
"openai>=1.0.0",
"opentelemetry-instrumentation-openai-v2>=2.0b0",
"opentelemetry-sdk>=1.30.0",
"posthog[otel]>=7.11.0",
]

[tool.uv.sources]
posthog = { path = "../.." }
897 changes: 897 additions & 0 deletions examples/example-ai-cloudflare-ai-gateway/uv.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/example-ai-cloudflare-ai-gateway/uv.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude-newer = "7 days"
3 changes: 3 additions & 0 deletions examples/example-ai-dedalus/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTHOG_API_KEY=phc_your_project_api_key
POSTHOG_HOST=https://us.i.posthog.com
DEDALUS_API_KEY=your_api_key
23 changes: 23 additions & 0 deletions examples/example-ai-dedalus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Dedalus Labs + PostHog AI Examples

Track Dedalus Labs API calls with PostHog via the OpenAI-compatible API.

## Setup

```bash
cp .env.example .env
# Fill in your API keys in .env
# Install uv if you haven't already: https://docs.astral.sh/uv/getting-started/installation/
uv sync
```

## Examples

- **chat.py** - Chat completions via Dedalus Labs

## Run

```bash
source .env
uv run python chat.py
```
44 changes: 44 additions & 0 deletions examples/example-ai-dedalus/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Dedalus Labs chat completions via OpenAI-compatible API, tracked by PostHog via OpenTelemetry."""

import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from posthog.ai.otel import PostHogSpanProcessor
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor

resource = Resource(
attributes={
SERVICE_NAME: "example-dedalus-app",
"posthog.distinct_id": "example-user",
"foo": "bar",
"conversation_id": "abc-123",
}
)
provider = TracerProvider(resource=resource)
provider.add_span_processor(
PostHogSpanProcessor(
api_key=os.environ["POSTHOG_API_KEY"],
host=os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com"),
)
)
trace.set_tracer_provider(provider)

OpenAIInstrumentor().instrument()

import openai # noqa: E402

client = openai.OpenAI(
base_url="https://api.dedaluslabs.ai/v1",
api_key=os.environ["DEDALUS_API_KEY"],
)

response = client.chat.completions.create(
Comment thread
richardsolomou marked this conversation as resolved.
model="openai/gpt-5-mini",
max_completion_tokens=1024,
messages=[
{"role": "user", "content": "Tell me a fun fact about hedgehogs."},
],
)

print(response.choices[0].message.content)
13 changes: 13 additions & 0 deletions examples/example-ai-dedalus/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "example-ai-dedalus"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
"openai>=1.0.0",
"opentelemetry-instrumentation-openai-v2>=2.0b0",
"opentelemetry-sdk>=1.30.0",
"posthog[otel]>=7.11.0",
]

[tool.uv.sources]
posthog = { path = "../.." }
Loading
Loading