---
title: Caching (self-hosted)
description: Langfuse provides caching for API keys and prompts to improve performance and reduce database load.
label: "Version: v4"
sidebarTitle: "Caching"
---

# Caching Features

Langfuse includes built-in caching capabilities for API keys and prompts to improve performance and reduce database load.
These features are enabled by default and can be configured or disabled as needed (starting with v3.81.0).

## Overview

Caching in Langfuse helps optimize performance by storing frequently accessed data in Redis, reducing the need for repeated database queries. This is particularly beneficial for:

- **API key validation**: Reduces database lookups for every API request
- **Prompt retrieval**: Speeds up prompt fetching for applications that frequently access the same prompts

## API Key Caching

API key caching stores validated API keys in Redis to avoid repeated database lookups during authentication.
This significantly improves response times for ingestion requests.
API keys are never stored in plaintext; only their hashed values are cached.

### Default Behavior

- **Status**: Enabled by default
- **TTL**: 300 seconds (5 minutes)
- **Scope**: All API key validations

### Configuration

| Variable                             | Default | Description                                 |
| ------------------------------------ | ------- | ------------------------------------------- |
| `LANGFUSE_CACHE_API_KEY_ENABLED`     | `true`  | Enable or disable API key caching           |
| `LANGFUSE_CACHE_API_KEY_TTL_SECONDS` | `300`   | Time-to-live for cached API keys in seconds |

### Disabling API Key Caching

To disable API key caching, set the following environment variable:

```bash
LANGFUSE_CACHE_API_KEY_ENABLED=false
```

## Prompt Caching

Prompt caching stores frequently accessed prompts in Redis to improve retrieval performance for applications that regularly fetch the same prompts.

### Default Behavior

- **Status**: Enabled by default
- **TTL**: 300 seconds (5 minutes)
- **Scope**: All prompt retrievals via API

### Configuration

| Variable                            | Default | Description                                |
| ----------------------------------- | ------- | ------------------------------------------ |
| `LANGFUSE_CACHE_PROMPT_ENABLED`     | `true`  | Enable or disable prompt caching           |
| `LANGFUSE_CACHE_PROMPT_TTL_SECONDS` | `300`   | Time-to-live for cached prompts in seconds |

### Disabling Prompt Caching

To disable prompt caching, set the following environment variable:

```bash
LANGFUSE_CACHE_PROMPT_ENABLED=false
```

## Cache Invalidation

Langfuse automatically handles cache invalidation to ensure data consistency:

- **API keys**: Cached API keys are invalidated when the key is deleted, disabled, or its permissions change
- **Prompts**: Cached prompts are invalidated when a prompt is updated, deleted, or a new version is published

## Migration from Previous Versions

  If you're upgrading from a previous version of Langfuse, these caching
  features will be automatically enabled with default settings. No additional
  configuration is required unless you want to customize the behavior.

---

If you experience any issues when self-hosting Langfuse, please:

1. Check out [Troubleshooting & FAQ](/self-hosting/troubleshooting-and-faq) page.
2. Use [Ask AI](/ask-ai) to get instant answers to your questions.
3. Ask the maintainers on [GitHub Discussions](/gh-support).
4. Create a bug report or feature request on [GitHub](/issues).

  Enterprise-grade support is available when self-hosting Langfuse. Learn more on
  our [pricing page](/pricing-self-host).

<!-- 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/self-hosting/configuration/caching.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>.
