> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chowder.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Caching

> Reuse expensive AI processing results and ephemeral nearest-neighbor indexes across queries.

Analytical inference workloads often revisit the same inputs with different filters, groupings, or joins. Chowder’s local-first, in-process engine reuses successful inference, embeddings, document parsing, and transcription results, along with eligible ephemeral nearest-neighbor indexes.

The calling engine manages that reuse whether AI processing runs locally, in fully managed Chowder Cloud, or on a self-hosted server. Change your analysis without repeatedly paying for the same AI processing or index construction.

> [!TIP]
>
> The reusable unit is the expensive operation and its inputs. Changing a downstream filter or aggregation can reuse the same AI results—even when the SQL query is different.

## Change the analysis, reuse the AI results

Suppose you want to identify product failures in Amazon reviews, then count how often they appear. Both queries below classify the same ten short reviews with the same prompt and output schema. Only the final analysis changes.

> [tab] Inspect the reviews
>
> ```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
> WITH reviews AS (
>     SELECT
>         title,
>         content
>     FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/amazon-reviews/*.parquet')
>     WHERE tokens(content) < 120 -- Keep this walkthrough small
>     ORDER BY title, content
>     LIMIT 10
> ), assessed AS (
>     SELECT
>         title,
>         infer(
>             content,
>             'Does the reviewer describe a product breaking or failing?',
>             BOOL
>         ).output AS product_failed
>     FROM reviews
> )
> SELECT
>     title,
>     product_failed
> FROM assessed;
> ```

> [tab] Count the results
>
> ```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
> WITH reviews AS (
>     SELECT
>         title,
>         content
>     FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/amazon-reviews/*.parquet')
>     WHERE tokens(content) < 120 -- Keep this walkthrough small
>     ORDER BY title, content
>     LIMIT 10
> ), assessed AS (
>     SELECT
>         title,
>         infer(
>             content,
>             'Does the reviewer describe a product breaking or failing?',
>             BOOL
>         ).output AS product_failed
>     FROM reviews
> )
> SELECT
>     product_failed,
>     count(*) AS review_count
> FROM assessed
> GROUP BY product_failed
> ORDER BY product_failed;
> ```
>
> The aggregation returns:
>
> | product\_failed | review\_count |
> | - | - |
> | false | 9 |
> | true | 1 |

The first query computes and retains successful classifications. The second can reuse those classifications and run the new aggregation without repeating the model calls. This also works after reopening an engine with the same result-cache directory, provided the entries remain available and their inputs and output settings still match.

Both queries execute as columnar plans over Arrow record batches. The `reviews` CTE fixes the logical input records before inference; it does not materialize a saved table. Cached classifications supply the `product_failed` column, while the second query executes its grouping and count over that column.

## What Chowder reuses

| Expensive operation | Reusable result | What must match |
| - | - | - |
| `infer` | Successful per-input AI results | Input content, model identity, prompt, output schema, and relevant output settings |
| `embed` | Per-input vector embeddings | Input content, model identity, and embedding settings |
| Document parsing | Parsed pages for a complete document | Source content and parser output identity |
| Audio and video processing | Compatible complete-source media results, including frames and transcripts when produced | Source content, processing identity, sampling, and required column coverage |

Reuse is automatic. You do not need to create a cache table or write a special SQL clause. Error results are not retained as successful AI responses. An `infer` result whose confidence couldn't be computed isn't cached either, so a later query retries it. Entries that exceed the cache budget may be left uncached without preventing the query from returning its result.

For document and media readers, a limited or interrupted query does not guarantee a reusable complete-source entry. Source verification may still require reading data even when processing results are cached. A compatible complete-media hit can replay frames and transcripts together without repeating decoding or transcription.

## When work runs again

Changing the prompt, model, input content, or requested output schema changes the inference operation. Existing results for the old operation do not answer the new one. Parser revisions and media processing settings similarly affect reuse.

Changing a downstream aggregation does not itself change an inference result. Adding more input rows computes results for missing inputs while eligible existing results remain reusable. Clearing, eviction, or unavailable entries means the affected work runs again.

This is reuse of expensive operations, rather than a cache of arbitrary SQL result tables. File scans, joins, filters, sorting, and aggregations still execute as required by the query.

## Reuse nearest-neighbor indexes

Chowder’s optimized nearest-neighbor operators build ephemeral indexes over embedding columns on demand for analytical queries and can reuse eligible indexes within the same engine session. The indexes are held in the calling engine’s memory and are not persisted across engine sessions. Repeated queries over unchanged, pointer-stable in-memory embedding columns can avoid rebuilding the index, including when the requested neighbor count changes.

Index reuse has a separate, bounded memory cache. It depends on the same source embedding buffers remaining available; do not assume reuse across fresh file scans or different engines. An oversized index may be rebuilt if it cannot be admitted to the memory budget.

See [Nearest-neighbor joins](https://docs.chowder.ai/guides/nearest-neighbor-joins.md) for the optimized lateral SQL pattern and execution modes.

## Configure and inspect the result cache

Caching is enabled automatically. Use `cache_dir` when you want engines to share a specific local result-cache directory:

```python theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
import chowder

engine = chowder.Engine(cache_dir=".chowder-cache")

# Execute and consume your queries with this engine.
print(engine.status()["cache"])

engine.close()
```

The cache directory must be on a writable local filesystem. Configure it on the calling `Engine`, including when processing runs on a self-hosted server or Chowder Cloud. Durable result-cache ownership stays with your application when you offload AI compute.

Reuse still requires matching input, model, and processing identities when you change compute destinations. See [Architecture](https://docs.chowder.ai/concepts/architecture.md#application-and-compute-boundaries) for the application and server boundaries.

| Setting | Purpose |
| - | - |
| `Engine(cache_dir=...)` | Set the result-cache directory; takes precedence over `CHOWDER_CACHE_DIR` |
| `CHOWDER_CACHE_GB` | Shared result-cache retention budget; a new root defaults to 8 GiB, and `0` means unbounded |
| `CHOWDER_KNN_INDEX_CACHE_MB` | Separate per-engine index-cache memory budget; defaults to 512 MiB |

An existing result-cache root retains its stored budget unless you explicitly configure a new one. The result-cache budget governs retained result payloads; it is not a total disk or process-memory limit.

## Results, models, and tokenizers have different lifetimes

Downloaded model weights and runtime binaries are retained separately from query results. Loaded tokenizers and resident model runtimes also have their own reuse rules. For example, `tokens()` can reuse a loaded tokenizer, but this does not mean every text's token count is stored in the durable result cache.

To remove cached results while preserving downloaded models and runtime artifacts:

```python theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
engine = chowder.Engine(cache_dir=".chowder-cache")

cleared = engine.clear_cache()
print(cleared["results"])

engine.close()
```

`clear_cache()` clears durable results; it does not clear the in-memory nearest-neighbor index cache or unload running models. `clear_cache(artifacts=True)` also removes Chowder-owned downloaded artifacts, so later use may need to download or rebuild them. Neither form clears the third-party Hugging Face cache.

See [Core concepts](https://docs.chowder.ai/concepts/overview.md) for the columnar data model, [Architecture](https://docs.chowder.ai/concepts/architecture.md) for execution and scheduling, and [Configuration](https://docs.chowder.ai/reference/configuration.md#cache-and-source-limits) for cache settings.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.