---
title: "Deploy Langfuse Pydantic AI"
description: "Trace Pydantic AI agents, tool calls and model requests"
category: "AI/ML"
url: https://railway.com/deploy/deploy-langfuse-for-pydantic-ai
---

# Deploy Langfuse Pydantic AI

Trace Pydantic AI agents, tool calls and model requests

**[Deploy Langfuse Pydantic AI on Railway](https://railway.com/template/deploy-langfuse-for-pydantic-ai)**

Machine-readable deploy manifest (JSON, validated by TemplateCI): https://railway.com/deploy/deploy-langfuse-for-pydantic-ai/manifest.json

- **Creator:** onepush
- **Category:** AI/ML

## Template content

### Postgres https://devicons.railway.app/i/postgresql.svg

- **Image:** ghcr.io/railwayapp-templates/postgres-ssl:18

### Redis https://cdn.sanity.io/images/sy1jschh/production/0ce0bfdcfbdbf69662b1116671f97c2dd788b655-157x157.svg

- **Image:** redis:8.2
- **Start command:** `/bin/sh -c "rm -rf $RAILWAY_VOLUME_MOUNT_PATH/lost+found/ && exec docker-entrypoint.sh redis-server --requirepass $REDIS_PASSWORD --save 60 1 --dir $RAILWAY_VOLUME_MOUNT_PATH"`

### ClickHouse

- **Image:** clickhouse/clickhouse-server:24.8

### langfuse-worker

- **Image:** langfuse/langfuse-worker:3

### langfuse-web

- **Image:** langfuse/langfuse:3
- **Public domain:** Yes

## Buckets

- **langfuse-bucket**

## Documentation

# Deploy and Host self hosted Langfuse Pydantic AI (Self-Hosted Tracing for Pydantic AI) on Railway

This template deploys a self-hosted Langfuse stack on Railway for Python teams building agents with Pydantic AI.

## About Hosting Langfuse Pydantic AI open-source software on Railway (self hosted Langfuse tracing for Pydantic AI agents template)

Langfuse is an open-source LLM observability platform, and this template runs the full self-hosted stack on Railway so your agent traces stay on infrastructure you control.

## Why Deploy Langfuse Pydantic AI, the Pydantic Logfire alternative on Railway (Railway Free Trial)

Pydantic Logfire has a free tier, but your agent traces live on its servers.

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying Langfuse Pydantic AI on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.

### Railway vs Other Hosting Providers and VPS for Langfuse Pydantic AI self hosting

Self-hosted Langfuse runs six services, so your hosting choice decides how much setup you do by hand.

| Provider | What You Get with Railway | What You Get with the Other Provider |
|---|---|---|
| DigitalOcean | The template deploys all six services with secrets generated per deploy. | A Droplet with root access, but you run Docker Compose and manage ClickHouse volumes and backups yourself. |
| AWS | The Storage Bucket is provisioned with the template, so no separate S3 setup. | Full networking and storage control, but IAM, VPC and S3 setup come first. |
| Hetzner | One click replaces the Compose file and secret generation steps. | Cheap RAM, but you need about 4 cores, 16 GiB RAM and 100 GiB disk, plus your own monitoring. |

Choose Railway if you want Langfuse running quickly, and a VPS if you need full control of the host.

## Common Use Cases for hosted Langfuse Pydantic AI

- **Finding slow or failing tool calls:** Each tool call appears as a child span under the agent run, showing which tool ran, how long it took and whether it errored.
- **Debugging failed structured outputs:** When validation fails, open the run in the Trace Table and inspect the agent's output next to the model request that produced it.
- **Attributing usage per user:** Attach user_id and session_id with propagate_attributes(...) so token usage and cost roll up per customer.
- **Keeping traces in your infrastructure:** Prompts and tool arguments often contain sensitive data, and self-hosting keeps those traces on servers you control.
- **Building evaluation datasets:** Pull representative production runs out of Langfuse and turn them into regression test cases for your agents.

## Dependencies for Langfuse Pydantic AI Docker hosted on Railway

This section covers the moving parts behind the template before you deploy it.

### Deployment Dependencies for Managed Langfuse Pydantic AI Service (LLM Observability)

langfuse-web (image langfuse/langfuse:3, public, port 3000) serves the UI and the OTel ingestion API your agents hit. langfuse-worker (image langfuse/langfuse-worker:3, port 3030) drains the queue and writes into ClickHouse 24.8, which stores traces, observations and scores on /var/lib/clickhouse.

### Implementation Details for Langfuse Pydantic AI (Using Langfuse official docker image)

DATABASE_URL points at Postgres, and REDIS_CONNECTION_STRING needs ?family=0 for Railway private IPv6.

## How does Langfuse Pydantic AI compare against other ways to trace Pydantic AI agent runs

### Langfuse Pydantic AI vs Pydantic Logfire (Pydantic Logfire Alternative)

- **Hosting model:** Logfire is a commercial hosted platform with a free tier. Langfuse Pydantic AI runs on your own servers, so traces never leave your infrastructure.
- **Instrumentation:** Both build on OpenTelemetry, so Agent.instrument_all() covers Pydantic AI either way.
- **Where Logfire wins:** Setup is shorter and there are no servers to run. If you don't want to operate ClickHouse and Redis, that matters.

### Langfuse Pydantic AI vs LangSmith (LangChain Tracing Alternative)

- **License:** LangSmith is closed source and self-hostable only on its Enterprise tier. Langfuse's core is MIT, and self-hosting the open-source version is free with no usage limits.
- **Framework fit:** LangSmith is built around LangChain. Langfuse Pydantic AI traces Pydantic AI agents directly through LANGFUSE_BASE_URL and your keys, with no LangChain dependency.
- **Where LangSmith wins:** If your stack is already LangChain, its native integration is less work.

### Langfuse Pydantic AI vs Arize Phoenix (Open Source Observability Alternative)

- **License:** Phoenix is source-available under the Elastic License 2.0, while Langfuse's core is MIT. That matters if your legal team reviews licenses closely.
- **Data model:** Phoenix is built on OpenTelemetry and OpenInference. Langfuse also ingests OpenTelemetry spans and adds a Trace Table for inspecting tool call spans and structured outputs.
- **Where Phoenix wins:** It is lighter to run locally, while a Langfuse self-host brings Postgres, Redis, ClickHouse and a storage bucket.

### Langfuse Pydantic AI vs Helicone (Proxy-First LLM Observability Alternative)

- **Capture point:** Helicone is proxy first. Langfuse Pydantic AI instruments the agent in-process, capturing tool calls and agent steps, not only raw model requests.
- **Open source:** Both are open source, so you can self-host either one.
- **Where Helicone wins:** If you only want request logging and cost tracking behind a single base URL change, a gateway is the quicker route.

## How to use Langfuse Pydantic AI (the OSS LLM observability platform)?

1. Deploy the Langfuse template on Railway and set LANGFUSE_BASE_URL to the public domain of langfuse-web.
2. Open the Langfuse UI, create a project and generate LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY on the project settings page.
3. Run `%pip install langfuse pydantic-ai -U`.
4. Export the three LANGFUSE_ variables as environment variables.
5. Call `langfuse = get_client()` and `langfuse.auth_check()` to confirm it returns True.
6. Call `Agent.instrument_all()` before creating agents.
7. Run your agent and call `langfuse.flush()` before the script exits.
8. Open the Trace Table to inspect tool call spans and structured outputs. If nothing appears, set LANGFUSE_DEBUG="True".

```python
from langfuse import get_client
from pydantic_ai import Agent

langfuse = get_client()
assert langfuse.auth_check()

Agent.instrument_all()
agent = Agent("openai:gpt-4o")

result = agent.run_sync("Say hello in one sentence.")
print(result.output)
langfuse.flush()
```

## How to self host Langfuse Pydantic AI on other VPS Services (Langfuse self hosting guide)

This path is for a plain Linux VPS you run yourself. Railway handles all of this for you, so skip it if you deployed the template.

### Clone the Repository

Clone the official Langfuse repository and move into it.

```bash
git clone https://github.com/langfuse/langfuse.git
cd langfuse
```

### Install Dependencies

You need Docker and Docker Compose on the VM. The official guidance is 4 cores, 16 GiB RAM and 100 GiB disk.

```bash
docker --version
docker compose version
```

### Configure Environment Variables

Copy the example env file and set `NEXTAUTH_URL` to your-domain.

```bash
cp .env.example .env
export LANGFUSE_BASE_URL="https://your-domain"
```

### Start the Langfuse Pydantic AI Application

Start the stack detached, then open the UI at your-domain to create a project and generate your API keys.

```bash
docker compose up -d
docker compose ps
```

Railway handles the managed services, networking and secrets for you, so Compose is only needed on a VPS you run yourself.

## Official Pricing of Langfuse Pydantic AI (Langfuse pricing)

The Langfuse software is MIT-licensed for the core repo, and self-hosting the OSS build is free with no usage limits.

Langfuse Cloud is the hosted option. Extra units cost $8 per 100k, and the `ee` folders need a license key.

## Langfuse Pydantic AI cloud vs self hosted comparison (Pricing, features, costs, and more)

Running Langfuse yourself trades a monthly bill for an operations job.

On Langfuse Cloud, the Hobby tier is free with 50k units per month, 30 days of retention and 2 users.

### Monthly cost of self hosting Langfuse Pydantic AI on Railway

Railway bills by resource usage, so treat the figure as a range. Watch the first month, since trace volume and storage drive the real number.

### System Requirements for Hosting Langfuse Pydantic AI on a VPS

If you move off Railway, Langfuse's self-hosting guidance suggests a VM with 4 cores, 16 GiB of RAM and 100 GiB of disk.

## Frequently Asked Questions (FAQs)

### Do tool calls and structured outputs show up as spans?

Yes. Both are visible in the Trace Table.

### Why are my spans missing in a short script?

Usually the process exits before export finishes. Also make sure `Agent.instrument_all()` runs before your agent code.

### How do I tag user_id and session_id on agent runs?

Wrap the run in a `@observe()` function and use `propagate_attributes(...)` to set `user_id`, `session_id`, tags, and metadata.

### Does instrument_all work with async agents?

Yes, async runs are traced the same way. Still call `langfuse.flush()` before your event loop shuts down.

### How do I filter unwanted spans from other OTel libraries?

Filter them before export, because unwanted spans still count toward billable units. The Langfuse docs cover this under "Unwanted spans in Langfuse."

### What data is stored in the Railway storage bucket and ClickHouse?

The Railway Storage Bucket holds raw events and media that langfuse-web uploads before the worker processes them.


## Similar templates

- [Chat Chat](https://railway.com/deploy/-WWW5r) — Chat Chat, your own unified chat and search to AI platform.
- [Weaviate](https://railway.com/deploy/weaviate-1) — Vector database with hybrid search and an API key-protected API
- [Möbius v0.1 Personal AI OS](https://railway.com/deploy/mobius-v01-personal-ai-os) — Self-hosted AI agent that builds and runs apps for you, with memory.

Open this page in a browser: https://railway.com/deploy/deploy-langfuse-for-pydantic-ai
