Skip to content
Cavemandocs
MIT

Framework middleware

Native framework compression with local recovery and honest diagnostics.

Native middleware projects eligible tool-result text into a smaller outbound model request. Your framework still owns inference, tools, retries, streams, and saved conversation. The local Caveman runtime compresses content; it does not forward these model calls.

The adapters are alpha. These guides use published TypeScript middleware 0.1.0-alpha.2, Python middleware 0.1.0a1, SDK 1.1.0, and the signed bin-v1.1.7 runtime. They do not require a Caveman account. Provider calls still require your provider credentials and can incur charges.

For an existing streaming Vercel AI SDK project, follow Add to Vercel AI SDK. For deterministic compression and exact-recovery checks, use the language quickstarts below.

Start with a complete run#

What happens automatically#

Once attached at a supported framework boundary, middleware considers successful text tool results on each model call. It sends eligible candidate text and context hashes to the configured runtime. In compress mode, it applies an accepted plan to a copied request. It leaves stored history intact. When recovery is required, the adapter must attest a real executable caveman_retrieve tool in the native loop.

It does not shorten every prompt, run your tools for you, select models, change provider prices, or guarantee better answers. User messages, system prompts, unsupported shapes, opaque content, and protected content remain unchanged. A short or incompressible result can produce no replacement. A tool schema alone is not a recovery executor.

IntegrationOperationWhere inference runs
Native middlewareAutomatic projection of eligible framework inputsYour existing provider client
ProxyBase-URL change; runtime forwards inferenceThrough the proxy
Thin SDKExplicit compression, provider, context, tool-search, or tracing callsAs configured for each explicit API
Agent SDKA separate agent runtime and package familyAccording to its agent configuration

Choose one owner for a model call. Do not stack a framework adapter, provider adapter, and proxy compression by default. Nested ownership guards are not a general guarantee across unrelated transports or services. The OpenAI and Anthropic TypeScript transport adapters have explicit cavemanProxy coordination for a discovered runtime origin; this does not make arbitrary mixed stacks supported.

Modes and final decisions#

The client defaults to compress; a freshly started standalone runtime defaults to record. Set both deliberately. A client requesting compression cannot force a recording runtime to replace text.

Client modeBehavior
offNative delegation; no optimization requests or receipts
recordCandidate observation, no text replacement
compressEligible replacements, with recovery when required

onReport / on_report receives a final decision after the adapter chooses its native request view. lastReport / last_report holds only the latest decision. Callback failures do not break inference.

Report statusMeaning
appliedAt least one newly selected replacement reached the native input
reusedAll replacements reused established choices for this scope
skippedNative originals retained; inspect reason
recordedRecording decision; originals retained
disabledMiddleware off

Reports include reason, transform IDs, replacement/reuse counts, adapter, logical call ID, and attempt ID. They contain no token counters. See measurement for the separate sources of segment estimates, provider usage, latency, and task success.

Continue by task#