Blog
guide

How to Connect PostHog to Claude Code Using Sequel

Musthaq Ahamad
Musthaq Ahamad
How to Connect PostHog to Claude Code Using Sequel

PostHog is where your product analytics live. Events, funnels, retention, cohorts, the lot. Claude Code is Anthropic's AI coding CLI that runs in your terminal. Sequel's MCP server connects the two, so you can ask about user behavior while you're still in the codebase that produced it.

This guide walks you through the full setup in four steps.

What You'll Accomplish

By the end of this guide, you'll be able to open a terminal, run Claude Code, and ask questions like:

  • "How many unique users hit the checkout flow yesterday, by country?"
  • "Show me the signup to activation funnel for users created in the last 30 days."
  • "What's the 7-day retention of the Power Users cohort?"

Claude Code will use Sequel's MCP server to write HogQL, run it against your PostHog project, and return the results in your terminal.

Prerequisites

  • A Sequel account: Sign up free at sequel.sh
  • A PostHog account on US Cloud, EU Cloud, or self-hosted
  • Your numeric project ID, from your PostHog URL: app.posthog.com/project/<id>
  • Claude Code installed: run npm install -g @anthropic-ai/claude-code if you haven't already

Step 1: Connect PostHog to Sequel

Sign in to sequel.sh and click Data Sources in the left sidebar.

Sequel Data Sources page

Click New Connection. On the "Choose a connector" page, select PostHog.

Choose a connector

Now create the key in PostHog. Go to Settings → Personal API Keys → Create personal API key and grant these six read-only scopes:

  • query:read
  • event_definitions:read
  • property_definitions:read
  • insight:read
  • cohort:read
  • project:read

Copy the key immediately. PostHog shows it exactly once.

Back in Sequel, fill in three fields:

  • API key: the personal API key you just copied
  • Host: https://us.posthog.com for US Cloud, https://eu.posthog.com for EU Cloud, or your self-hosted base URL with no trailing slash
  • Project ID: the number from your PostHog URL

Click Connect. Sequel verifies the credentials against your project and confirms it by name.

Tip: grant read scopes only. The connector has no write tools, so a read-only key means Claude Code cannot touch your insights, dashboards, cohorts, or feature flags even by accident.

Step 2: Get Your Sequel API Key

Click Settings in the left sidebar, then select API Keys.

Sequel API Keys page

Click New key, name it claude-code-posthog, and copy the key. It starts with sql_. Treat it like a password.

Shortcut: if you have the Sequel CLI installed, sequel install claude-code creates the key and writes the Claude Code config for you. That covers Steps 2 and 3 in one command.

Step 3: Configure Claude Code to Use Sequel MCP

In your terminal:

claude mcp add --transport http sequel https://api.sequel.sh/mcp \
  --header "Authorization: Bearer sql_your_api_key"

Replace sql_your_api_key with the key from Step 2. Add --scope user before --transport to install it globally for every project. Verify with:

claude mcp list

You should see sequel in the list of configured MCP servers.

Step 4: Query Your PostHog Data

Start a Claude Code session and try these:

How many unique users triggered a pageview yesterday, broken down by country?
Build the signup to first-query funnel for users who signed up in the last 30 days, and tell me where the biggest drop-off is.
What's the week-over-week trend in daily active users for the last 8 weeks?

Claude Code calls the Sequel MCP server, which writes the HogQL, runs it against your project, and returns the results inline.

What You Can Do Now

  • Ship-and-check loops: after deploying a feature, ask "How many users have triggered the new export event since yesterday?" without leaving the terminal.
  • Funnel debugging: ask "Which step of the onboarding funnel lost the most users last week compared to the week before?"
  • Cohort analysis: ask "Compare 30-day retention between users who connected a data source in week one and those who didn't."
  • Event hygiene: ask "Which tracked events have had zero volume in the last 30 days?" to find dead instrumentation before it rots.
  • Cross-source analysis: with Postgres or Stripe also connected, ask "For accounts on the Pro plan in Stripe, what's their 30-day active usage in PostHog?" That join is the part PostHog cannot answer on its own.

Try Sequel

Meet your always-on data analyst.

An AI data analyst that connects to all your data and answers questions with reports and visualizations. Free for up to 3 seats - no credit card required.

Get started free

Frequently asked questions

Do I need to know HogQL to use this?

No. You ask in plain English and Sequel writes the HogQL. It reads your event and property definitions first, so it composes queries against the events you actually track rather than guessing at names.

What scopes does the PostHog Personal API key need?

Six read-only scopes are enough - query:read, event_definitions:read, property_definitions:read, insight:read, cohort:read, and project:read. Granting only read scopes means Sequel cannot create or change insights, dashboards, cohorts, or feature flags.

Where do I find my PostHog project ID?

It is the number in your PostHog URL. Open your project and look at app.posthog.com/project/12345 - the 12345 is the project ID Sequel needs. It is numeric, not the project name.

Does this work with EU Cloud or self-hosted PostHog?

Yes. Set the host field to https://eu.posthog.com for EU Cloud, https://us.posthog.com for US Cloud, or your own base URL if you self-host. Leave off the trailing slash. If queries come back empty or hit the wrong project, the host is the first thing to check.

Can Claude Code modify my PostHog insights or feature flags?

No. The connector ships read tools only - HogQL query, list events, list properties, list insights, and list cohorts. There is no write path, and a read-only Personal API key closes the door a second time.

Can I query saved insights and cohorts I already built in PostHog?

Yes. Sequel can list the trends, funnels, and retention insights your team has saved, and it can reference saved cohorts inside HogQL as person_id IN COHORT. That means you can reuse existing segment definitions instead of redefining them in a prompt.

Written by

Musthaq Ahamad
Musthaq Ahamad

Co-founder and CEO of Sequel. Previously built developer tools and data infrastructure. Passionate about making data accessible for everyone.