IntegrationsOtherClaude Code

Claude Code Tracing with Langfuse

What is Claude Code?: Claude Code is Anthropic’s agentic coding tool that lives in your terminal. It can understand your codebase, help you write and edit code, execute commands, create and run tests, and help you accomplish complex coding tasks with natural language. Claude Code brings the power of Claude’s AI capabilities directly into your development workflow.

What is Langfuse?: Langfuse is an open-source LLM engineering platform. It helps teams trace LLM applications, debug issues, evaluate quality, and monitor costs in production.

Why Trace Claude Code?

  • Add context to code changes. Every git commit includes a link to the Langfuse session that produced it, showing the full inner workings of Claude Code — prompts, tool calls, reasoning, and more.
  • Track costs. Monitor how much your Claude Code interactions cost across projects and sessions.
  • Monitor latency. Observe whether Claude Code response times degrade at certain times of day (e.g. when US usage peaks).

What Can This Integration Trace?

By using the Langfuse CLI, this integration installs Claude Code hooks that capture full conversation interactions and sends them to Langfuse. You can monitor:

  • User inputs: Every prompt and message you send to Claude Code
  • Assistant responses: Claude’s responses and reasoning
  • Tool invocations: When Claude Code uses tools like file editing, bash commands, or web searches
  • Tool inputs and outputs: Data passed to and returned from each tool
  • Session information: Related interactions grouped into logical sessions
  • Timing information: How long operations take
  • Git integration: Each commit links back to the Langfuse session, and each Langfuse trace links to the git commit

Quick Start

Set up Langfuse

  1. Sign up for Langfuse Cloud or self-host Langfuse.
  2. Create a new project and copy your API keys from the project settings.

Install the Langfuse CLI

Install the Langfuse CLI globally:

npm i -g langfuse-cli

Or run it directly via npx:

npx langfuse-cli

Configure Environment Variables

Create an .env file in your project root (or any convenient location) with your Langfuse API keys. You can get the values from your Langfuse project settings.

LANGFUSE_PUBLIC_KEY=pk-lf-...
LANGFUSE_SECRET_KEY=sk-lf-...
LANGFUSE_HOST=https://cloud.langfuse.com  # optional, this is the default
# Use https://us.cloud.langfuse.com for the US region

Enable Claude Code Integration

Run the following command to enable tracing:

langfuse --env .env integration claudecode enable

This command installs two Claude Code hooks:

  1. Stop hook: Runs after each Claude Code response to send the conversation turn to Langfuse as a trace.
  2. Pre-commit hook: Attaches a link to the Langfuse session to every git commit message, so you can navigate from a commit directly to the session that produced it.

Use Claude Code

Start using Claude Code in your project as you normally would:

cd your-project
claude

All conversation turns are automatically traced to Langfuse. Every git commit made by Claude Code will include a link to the corresponding Langfuse session.

See Traces in Langfuse

Open your Langfuse project to see the captured traces. You’ll see:

  • Turn traces: Each conversation turn (user prompt → assistant response) as a trace
  • Generation spans: Claude’s LLM responses with model info
  • Tool spans: Nested spans for each tool call (Read, Write, Bash, etc.)
  • Session grouping: All turns from the same session are grouped via session_id
  • Git commit links: Traces link back to the git commit, and commits link to the Langfuse session

Claude Code Example Trace in Langfuse

Public example trace in Langfuse

Disable Tracing

To remove the Claude Code hooks and stop tracing:

langfuse integration claudecode disable

Troubleshooting

No traces appearing in Langfuse

  1. Verify the integration is enabled by checking your Claude Code hook configuration at ~/.claude/settings.json.
  2. Verify environment variables — ensure your .env file has the correct API keys (public key starts with pk-lf-).
  3. Check the Langfuse host matches your region:
    • EU region: https://cloud.langfuse.com
    • US region: https://us.cloud.langfuse.com

Re-running the setup

If something went wrong, disable and re-enable the integration:

langfuse integration claudecode disable
langfuse --env .env integration claudecode enable

Resources

Was this page helpful?