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

> ## Agent Instructions
> TabPFN-3 and later versions support up to 1,000,000 rows, subject to feature count and checkpoint/API limits.
> For large datasets, use per-estimator subsampling, e.g. inference_config={"SUBSAMPLE_SAMPLES": 100_000}, in TabPFNClassifier or TabPFNRegressor from tabpfn or tabpfn_client.
> Pass raw pandas DataFrames to fit and predict. Categorical strings/categories, raw text, and missing feature values are handled automatically; no manual integer/one-hot encoding, imputation, scaling, or outlier removal is needed.

# Faster Inference with KV Cache

> Get a quick introduction into faster inference with fit_with_cache

By
Prior Labs[LinkedIn](https://www.linkedin.com/company/prior-labs)[X](https://twitter.com/prior_labs)

[Open in Colab](https://colab.research.google.com/github/PriorLabs/tabpfn-cookbook/blob/main/notebooks/faster_performance_with_cache.ipynb)

*Build the model's internal representation once at fit time, reuse it at predict.*

By default TabPFN recomputes its representation of the training set on every call to `predict`. The `fit_with_cache` mode does that work once, during `fit`, and caches the result, so the first prediction is noticeably faster. This short notebook benchmarks the two modes side by side on the same data.

## Setup

*Installing TabPFN and scikit-learn.*

```python theme={null}
!pip install tabpfn scikit-learn
```

## Imports

*The classifier, a dataset, and timing utilities.*

```python theme={null}
import time
import os

from google.colab import userdata
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

from tabpfn import TabPFNClassifier
```

## Authenticating with a Token

*Setting `TABPFN_TOKEN` so the weights can be downloaded.*

```python theme={null}
os.environ["TABPFN_TOKEN"] = userdata.get('TABPFN_TOKEN')
```

## Data and Split

*A synthetic 5,000-row classification set; a small test slice keeps prediction quick.*

```python theme={null}
X, y = make_classification(n_samples=5000, n_features=20, random_state=42, n_classes=2)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.05, random_state=42, stratify=y
)
```

## The Benchmark Function

*Timing fit and predict.*

`benchmark_tabpfn` fits, times the first prediction. The first prediction is where the cache pays off.

```python theme={null}
def benchmark_tabpfn(clf: TabPFNClassifier, name: str, n_predict_calls = 1) -> None:
    t0 = time.perf_counter()
    clf.fit(X_train, y_train)
    t_fit = time.perf_counter() - t0

    t1 = time.perf_counter()
    for _ in range(n_predict_calls):
        preds = clf.predict(X_test)
    t_pred = time.perf_counter() - t1

    print(
        f"[{name}] fit: {t_fit:.4f}s | predict: {t_pred:.4f}s"
    )
```

## With and Without the Cache

*Comparing default mode against `fit_with_cache`.*

The baseline recomputes the training representation at predict time. With `fit_with_cache`, that work happens during `fit`, so the first prediction is faster.

The very first `fit` in a session also loads the model weights, so we run one untimed warm-up fit first to keep that out of the numbers.

```python theme={null}
# Warm-up: loads the model weights so the timings below exclude that one-off cost
TabPFNClassifier().fit(X_train, y_train)

# Baseline: no cache
clf_no_cache = (
    TabPFNClassifier()
)
benchmark_tabpfn(clf_no_cache, "no_cache")

# With KV cache: cache is built during `fit`, so first predict is faster
clf_kv = TabPFNClassifier(fit_mode="fit_with_cache")
benchmark_tabpfn(clf_kv, "kv_cache")
```

```console theme={null}
[no_cache] fit: 1.4601s | predict: 0.9072s
[kv_cache] fit: 1.3568s | predict: 0.1653s
```

## The Importance of Cache with Repeated `predict` Calls

```python theme={null}
N_PREDICT_CALLS = 10

# Baseline: no cache
clf_no_cache = (
    TabPFNClassifier()
)
benchmark_tabpfn(clf_no_cache, "no_cache", n_predict_calls = N_PREDICT_CALLS)

# With cache: the multiple predict calls are easily handled
clf_kv = TabPFNClassifier(fit_mode="fit_with_cache")
benchmark_tabpfn(clf_kv, "kv_cache", n_predict_calls = N_PREDICT_CALLS)
```

```console theme={null}
[no_cache] fit: 1.1413s | predict: 4.2302s
[kv_cache] fit: 1.3403s | predict: 1.5691s
```


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