Skip to content

Caching and rate limits

Two settings help when the same documents come back, or when many processes share one model account. Both are off by default, and neither changes what a result contains.

  • Result cache: a file that was already processed with the same request returns the stored result right away instead of being extracted again.
  • Rate limit: model calls wait their turn so that the API and all workers together stay under your provider's requests-per-minute and tokens-per-minute limits.

With QAECHI_REDIS_URL set, both are shared by every process and host. Without it, the cache uses your database (or memory) and each process limits itself.

pip install "qaechi-parser[redis]"
export QAECHI_REDIS_URL=redis://localhost:6379/0
export QAECHI_CACHE=on
export QAECHI_LLM_RPM=500 QAECHI_LLM_TPM=200000

Any Redis 7+ compatible server works: Redis, Valkey, Amazon ElastiCache, Upstash. Use rediss:// for TLS.

Result cache

Turn it on with QAECHI_CACHE=on. Before extracting, qaechi-parser computes a key from:

  • the file content (a SHA-256 hash; the file name and documentId do not matter, so the same file uploaded twice is a hit),
  • docType, the requested fields, lines, period and sourceKind,
  • the extraction settings: model (QAECHI_LLM_MODEL), prompt version, retrieval settings and plugin versions,
  • the qaechi-parser version, so an upgrade never serves a result made by older code.

Change any of these and the key changes, so a stale entry is never served.

On a hit the job still gets its own result: a new id, the job's jobId and documentId (also in every provenance.documentId), and a line in notes:

"notes": ["cache: reused result 9b1e... from 2026-11-30T09:12:44+00:00 (same file and request)"]

Values, quotes, pages and groundedness are the ones stored with the original result. Results with stub: true are never cached.

Where entries live

Configured Cache backend Shared across processes
QAECHI_REDIS_URL Redis, keys qaechi:cache:* with a TTL Yes
QAECHI_DATABASE_URL The same database as jobs and results: qaechi_cache table (PostgreSQL, MySQL) or collection with a TTL index (MongoDB) Yes
Neither Memory of each process, bounded by QAECHI_CACHE_MAX_ENTRIES No

A custom store set with set_store() is used for the cache when it has cache_get(key) and cache_put(key, result, ttl_seconds) methods. If the cache backend is unreachable, the lookup counts as a miss and the job runs normally.

GET /health shows the active backend in cache (off, redis, postgres, mysql, mongodb or memory).

Rate limit for model calls

Set QAECHI_LLM_RPM, QAECHI_LLM_TPM or both. Each model gets two token buckets that refill evenly over a minute: one call takes one request, and its estimated size (from the input length) in tokens. A call that does not fit waits until it does.

  • Shared: with QAECHI_REDIS_URL, the buckets live in Redis and are updated atomically, so the limit holds for the API and every worker together. If Redis becomes unreachable, each process falls back to its own buckets and logs a warning.
  • Per process: without Redis, each process has its own buckets. Divide your provider's limits by the number of processes (for example QAECHI_WORKER_PROCESSES x replicas).
  • 429 backoff: if the provider still answers 429 Too Many Requests, the call is retried with exponential backoff and jitter, never sooner than the provider's Retry-After. Every process that shares the buckets pauses for the same time.
  • Bounded wait: a call that would wait longer than QAECHI_LLM_MAX_WAIT_SECONDS fails the job with a clear error instead of hanging.

Set the limits a little below what your provider grants: the token count is an estimate made before the call. GET /health shows the limiter in llm_rate_limit (off, local or redis).

Settings

Variable Default Purpose
QAECHI_REDIS_URL Redis for the shared cache and rate limit, e.g. redis://redis:6379/0 ([redis] extra)
QAECHI_CACHE off on to reuse results for the same file and request
QAECHI_CACHE_TTL_SECONDS 86400 How long a cached result is kept
QAECHI_CACHE_MAX_ENTRIES 1000 Entries per process for the in-memory cache
QAECHI_LLM_RPM 0 (off) Model requests per minute, per model
QAECHI_LLM_TPM 0 (off) Model tokens per minute, per model
QAECHI_LLM_MAX_WAIT_SECONDS 300 Longest a call waits for the limit before the job fails
QAECHI_LLM_RATE_LIMIT_RETRIES 4 Retries after a 429 answer

Metrics

With telemetry on:

Metric Labels Answers
qaechi_cache_lookups_total outcome hit, miss, error: how often results are reused
qaechi_llm_throttled_total call, reason Model calls that had to wait. call is chat or embed; reason is limit (the configured RPM/TPM) or 429 (the provider)
qaechi_llm_throttle_wait_seconds (histogram) call, reason How long they waited

A steady rise in reason="429" means the configured limits are higher than what the provider grants.

한국어