---
title: Trace Swiftide with Langfuse
sidebarTitle: Swiftide
logo: /images/integrations/swiftide.png
description: Learn how to use Langfuse to monitor Swiftide applications to debug and evaluate your LLM workflows
category: Integrations
---

# Tracing Swiftide with Langfuse

This guide demonstrates how to **integrate Langfuse** into your **Swiftide** workflows to monitor, debug, and evaluate your LLM applications.

> **What is Swiftide?**: [Swiftide](https://github.com/bosun-ai/swiftide) is a Rust library for building LLM applications. It provides simple primitives for prompt completion, streaming indexing and querying pipelines, and agents that can use tools or call other agents. It has integrations with popular LLMs and storage providers, a modular API, and first-class support for tracing and Langfuse. Documentation is available at [swiftide.rs](https://swiftide.rs).

<Steps>

## Install Dependencies

Enable the `langfuse` feature when adding Swiftide to your `Cargo.toml`:

```toml
swiftide = { version = "0.31", features = ["langfuse", "openai"] }
```

Also add `tracing-subscriber` to configure tracing:

```toml
tracing-subscriber = "0.3"
```

## Configure Environment & Langfuse Credentials

Set the required environment variables for Langfuse. You can obtain these keys from the [Langfuse Cloud](https://langfuse.com/cloud) project settings page or by [self-hosting Langfuse](https://langfuse.com/self-hosting).

```bash
export LANGFUSE_PUBLIC_KEY=pk-lf-...
export LANGFUSE_SECRET_KEY=sk-lf-...
# optional, defaults to http://localhost:3000
export LANGFUSE_URL=https://cloud.langfuse.com
```

## Instrumenting Swiftide with Langfuse

Swiftide integrates with `tracing`. To send traces to Langfuse, set up the `LangfuseLayer` alongside your normal tracing layers.

```rust
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{
    EnvFilter, Layer as _, layer::SubscriberExt as _, util::SubscriberInitExt as _,
};
use swiftide::langfuse::LangfuseLayer;

let fmt_layer = tracing_subscriber::fmt::layer()
    .compact()
    .with_target(false)
    .boxed();

let langfuse_layer = LangfuseLayer::default()
    .with_filter(LevelFilter::DEBUG)
    .boxed();

let registry = tracing_subscriber::registry()
    .with(EnvFilter::from_default_env())
    .with(vec![fmt_layer, langfuse_layer]);

registry.init();
```

Once this is set up, any instrumented spans or Swiftide operations will be reported to Langfuse.

## Hello World Example

Here is a minimal Swiftide program with Langfuse enabled. It sends a simple prompt to OpenAI through Swiftide and logs the trace to Langfuse.

```rust
//! This is an example of using the langfuse integration with Swiftide.
//!
//! Langfuse is a platform for tracking and monitoring LLM usage and performance.
//!
//! When the feature `langfuse` is enabled, Swiftide can report tracing information,
//! usage, inputs, and outputs to langfuse.
//!
//! For this to work, you need to set the LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY
//! to the appropriate values. You can also set the LANGFUSE_URL environment variable
//! to overwrite the default URL (http://localhost:3000).
use anyhow::Result;
use swiftide::traits::SimplePrompt;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{
    EnvFilter, Layer as _, layer::SubscriberExt as _, util::SubscriberInitExt as _,
};

#[tokio::main]
async fn main() -> Result<()> {
    println!("Hello, langfuse!");

    let fmt_layer = tracing_subscriber::fmt::layer()
        .compact()
        .with_target(false)
        .boxed();

    let langfuse_layer = swiftide::langfuse::LangfuseLayer::default()
        .with_filter(LevelFilter::DEBUG)
        .boxed();

    let registry = tracing_subscriber::registry()
        .with(EnvFilter::from_default_env())
        .with(vec![fmt_layer, langfuse_layer]);

    registry.init();

    prompt_openai().await?;

    Ok(())
}

#[tracing::instrument]
async fn prompt_openai() -> Result<()> {
    let openai = swiftide::integrations::openai::OpenAI::builder()
        .default_prompt_model("gpt-5")
        .build()
        .unwrap();

    let paris = openai
        .prompt("What is the capital of France?".into())
        .await?;

    println!("The capital of France is {paris}");

    Ok(())
}
```

</Steps>

Running this program will create a Langfuse trace for the prompt, including input, output, usage, and metadata.

## Learn More

For more details on using Swiftide, check out the following resources:

- [Swiftide on GitHub](https://github.com/bosun-ai/swiftide)
- [Swiftide documentation](https://swiftide.rs)

<!-- agent-instructions -->

---

## Agent Instructions

This page is part of the [Langfuse](https://langfuse.com) documentation, published as plain Markdown for AI agents. Every page is available as Markdown by appending `.md` to its URL, or by sending an `Accept: text/markdown` header. This page: `https://langfuse.com/integrations/frameworks/swiftide.md`.

### Querying these docs

If the answer is not on this page, query the documentation instead of guessing:

- **Semantic search** across all Langfuse docs, returning an answer with the relevant pages and excerpts. Ask a specific, self-contained question:

  ```bash
  curl -sG "https://langfuse.com/api/search-docs" --data-urlencode "query=How do I trace a LangGraph agent?"
  ```

- **Index of every page**: <https://langfuse.com/llms.txt>, with per-section indexes [llms-docs.txt](https://langfuse.com/llms-docs.txt), [llms-integrations.txt](https://langfuse.com/llms-integrations.txt), and [llms-self-hosting.txt](https://langfuse.com/llms-self-hosting.txt).

### Before writing Langfuse code

- **Install the [Langfuse Agent Skill](https://langfuse.com/docs/api-and-data-platform/features/agent-skill).** It encodes Langfuse's own best practices for instrumentation, prompt management, and evaluation, and materially improves results.
- **Read [What does a good trace look like?](https://langfuse.com/docs/observability/best-practices.md)** before instrumenting an application.
- **Verify endpoints, parameters, and response fields** against the [API reference](https://api.reference.langfuse.com) instead of inferring them from code examples.
- **Use the [Langfuse CLI](https://langfuse.com/docs/api-and-data-platform/features/cli)** (`npx langfuse-cli api <resource> <action>`) to read or write traces, prompts, datasets, and scores from the terminal.

Found an error in these docs? Please open an issue at <https://github.com/langfuse/langfuse-docs/issues>.
