How to Configure Parse Result Caching in RAGAnything for Improved Performance

Enable persistent parse result caching in RAGAnything by setting a stable WORKING_DIR environment variable and reusing consistent parser configurations—this automatically stores parsed documents in a LightRAG KV store and eliminates redundant OCR and extraction operations.

Parse result caching is a core performance optimization in RAGAnything (HKUDS/RAG-Anything). When you process documents repeatedly—whether during development, testing, or production reruns—the system automatically skips expensive parsing operations by retrieving previously computed results from a dedicated cache store. This article explains how to configure and optimize this caching mechanism based on the actual implementation in the RAGAnything source code.

Understanding the Parse Cache Architecture

RAGAnything implements parse result caching through a LightRAG KV store named parse_cache. This storage layer persists parsed document content, metadata, and configuration hashes to disk, enabling cache survival across process restarts.

The caching system operates automatically once initialized. When you invoke document parsing, RAGAnything generates a deterministic cache key from your file and parser configuration. If this key exists in parse_cache and the file hasn't changed, the stored result returns immediately. Otherwise, the parser executes and the result is cached for future use.

Core Components in the Source Code

Component Purpose Source Location
RAGAnythingConfig Global configuration including working_dir for cache persistence raganything/config.py#L18-L20
RAGAnything._ensure_lightrag_initialized Creates LightRAG instance and initializes parse_cache KV store raganything/raganything.py#L68-L76
ProcessorMixin._generate_cache_key Builds MD5 hash key from file path, mtime, parser, and kwargs raganything/processor.py#L44-L92
ProcessorMixin._get_cached_result Validates and retrieves cached entries with mtime/config checks raganything/processor.py#L33-L66
ProcessorMixin._store_cached_result Persists parsed content, doc_id, mtime, and configuration raganything/processor.py#L12-L28
ProcessorMixin.parse_document Orchestrates cache lookup, miss handling, and result storage raganything/processor.py#L80-L98

Configuring Cache Persistence with WORKING_DIR

The most critical configuration for parse result caching is the working_dir setting. This determines where the LightRAG KV store—including the parse_cache folder—is persisted to disk.

Environment Variable Configuration

import os
from raganything.raganything import RAGAnything

# Set persistent storage location via environment variable

os.environ["WORKING_DIR"] = "/data/rag_storage"

rag = RAGAnything(
    llm_model_func=my_llm,
    embedding_func=my_embed,
)

The WORKING_DIR environment variable is read in raganything/config.py and falls back to ./rag_storage if unset. For production deployments, always specify an absolute path to a durable storage volume.

Direct Configuration Override

from raganything.config import RAGAnythingConfig
from raganything.raganything import RAGAnything

# Explicit configuration object

config = RAGAnythingConfig(
    working_dir="/mnt/persistent/rag_cache",
    use_full_path=True,  # Also configure path handling

)

rag = RAGAnything(
    config=config,
    llm_model_func=my_llm,
    embedding_func=my_embed,
)

Optimizing Cache Key Generation for Maximum Hits

Cache hit rate depends entirely on consistent cache key generation. The _generate_cache_key method in raganything/processor.py creates an MD5 hash from several components—any variation produces a different key and triggers a cache miss.

Components of the Cache Key

Component Description Configurable?
File path Absolute or relative path to the document Yes—via USE_FULL_PATH
File modification time (mtime) Timestamp of last file change No—automatically detected
Parser name Selected parser (e.g., minerU, unstructured) Yes—via PARSER env/var
Parse method Parsing strategy (e.g., auto, fast, accurate) Yes—via PARSE_METHOD
Parser kwargs Additional parameters like lang, device, start_page Yes—passed to parse_document()

Controlling Path Sensitivity with USE_FULL_PATH

When the same filename exists in multiple directories, enable USE_FULL_PATH to prevent false cache hits between different files:

import os
from raganything.raganything import RAGAnything

os.environ["WORKING_DIR"] = "/data/rag_storage"
os.environ["USE_FULL_PATH"] = "true"

rag = RAGAnything(
    llm_model_func=my_llm,
    embedding_func=my_embed,
)

# These will be cached separately despite identical basenames

content_a, _ = await rag.parse_document("deptA/report.pdf")
content_b, _ = await rag.parse_document("deptB/report.pdf")

The USE_FULL_PATH setting is defined in raganything/config.py and consumed by _generate_cache_key in raganything/processor.py.

Maintaining Stable Parser Configurations

For maximum cache effectiveness, keep parser settings identical across runs:


# First run—cache miss, parses the PDF

content1, doc_id1 = await rag.parse_document(
    "annual_report.pdf",
    parser="minerU",           # Must match for cache hit

    parse_method="auto",       # Must match for cache hit

    lang="en",                 # Must match for cache hit

    device="cpu",              # Must match for cache hit

)

# Second run—cache hit, instant retrieval

content2, doc_id2 = await rag.parse_document(
    "annual_report.pdf",
    parser="minerU",
    parse_method="auto",
    lang="en",
    device="cpu",
)

assert content1 == content2  # True—retrieved from cache

assert doc_id1 == doc_id2    # Same document ID

Managing Cache Contents Directly

For debugging, inspection, or manual intervention, you can interact with the parse_cache store directly through the public API exposed by RAGAnything.

Inspecting Cache Entries


# Access the parse_cache KV store instance

cache = rag.parse_cache  # LightRAG key_string_value_json_storage_cls

# List all cache keys (MD5 hashes)

keys = await cache.keys()
print(f"Cache contains {len(keys)} parsed documents")

# Retrieve specific entry (useful for debugging)

entry = await cache.get("3f2a9c7e5d...")
print(f"Stored mtime: {entry['mtime']}")
print(f"Parser config: {entry['parse_config']}")

Removing Individual Entries


# Delete a specific cache entry by its key

await cache.delete("3f2a9c7e5d...")

Complete Cache Reset

import shutil
import pathlib
import os

# Determine cache location from environment or config

working_dir = os.getenv("WORKING_DIR", "./rag_storage")
cache_path = pathlib.Path(working_dir) / "parse_cache"

# Remove entire cache directory

if cache_path.exists():
    shutil.rmtree(cache_path)
    print(f"Removed cache at {cache_path}")

# Re-initialize RAGAnything to create fresh cache

rag = RAGAnything(
    llm_model_func=my_llm,
    embedding_func=my_embed,
)

Environment Variable Reference

Variable Default Purpose
WORKING_DIR ./rag_storage Root directory for LightRAG storage and parse_cache
USE_FULL_PATH false Include full absolute path in cache key to distinguish same-named files in different directories
PARSER minerU Default parser selection (affects cache key generation)
PARSE_METHOD auto Default parsing method (affects cache key generation)

These variables are defined and parsed in raganything/config.py and consumed throughout the caching pipeline.

Summary

  • Parse result caching in RAGAnything is automatic once enabled through a persistent working_dir configuration.
  • Set WORKING_DIR to a durable location via environment variable or direct configuration to ensure cache survival across restarts.
  • Optimize cache hit rates by maintaining stable parser configurations and using USE_FULL_PATH when identical filenames exist in different directories.
  • Cache keys incorporate file path, modification time, parser name, parse method, and all parser-specific kwargs—any variation triggers a fresh parse.
  • Direct cache management is available through rag.parse_cache for inspection, selective deletion, or complete reset via filesystem operations.

Frequently Asked Questions

What happens if I change the parser but keep the same file?

Changing the parser name, parse method, or any parser-specific kwargs automatically generates a new cache key. The system will treat this as a cache miss and perform a fresh parse. Both results are stored independently in the cache, so you can switch between parser configurations without losing previously cached results.

How does RAGAnything detect if a file has been modified?

The cache key includes the file's modification time (mtime) retrieved from the filesystem. When _get_cached_result retrieves a cached entry, it validates that the stored mtime matches the current file mtime. If the file has been modified since the original parse, the cache entry is rejected and a new parse is executed.

Can I disable parse result caching entirely?

There is no explicit configuration flag to disable caching. However, you can effectively bypass the cache by either: (1) using a temporary working_dir that is deleted after each run, (2) manually clearing the cache between operations, or (3) introducing intentional variation in parser kwargs to generate unique cache keys for every invocation.

Where is the cache physically stored on disk?

The cache resides in a directory named parse_cache under your configured working_dir. The exact path is {working_dir}/parse_cache. This directory contains LightRAG's key-value storage files. For persistent caching across system restarts, ensure working_dir points to a location on durable storage that is not ephemeral or containerized without volume mounts.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →