Skip to content

Caveman Cloud· HostedTutorials

Control Caveman Gateway optimizations per request

Choose record mode or active optimizations per request with x-cave-optimize and related headers. Understand receipts, opt-out, and what each optimizer does.

The Caveman gateway can pass requests through byte-identical or apply eligible optimizations on the fly. You control the behavior of every single call with request headers. This guide explains how to opt in, opt out, read the receipt, and understand which optimizers exist and when they run.

Record mode vs optimizations#

By default, the gateway runs eligible byte-safe optimizations (S0 and S1). Behavioral and structural changes require opt-in and may need evaluation gates. A single header overrides the behavior for one request.

ModeHow to requestWhat happens
Recordx-cave-optimize: offPass through byte-identical. No optimization runs.
DefaultOmit the headerRun eligible byte-safe optimizations (prompt cache hints, routing hints).
Opt-upx-cave-optimize: compress or cache-hintsRequest a specific optimization; the gateway checks policy, capability, and eval gates before applying it.
Opt-downx-cave-optimize: no-cacheDisable one optimization for this request while leaving others running.

The x-cave-optimize header#

x-cave-optimize accepts comma-separated tokens. Mixing opt-up and opt-down in one header is allowed; opt-down wins when the same optimization is named.

TokenMeaning
offPass through byte-identical
no-cacheDisable prompt cache hints
no-compressDisable compression
no-routeDisable model routing
no-stream-usageOn streamed OpenAI chat, do not request token usage from OpenAI
compressRequest compression
compress=losslessRequest compression that changes no value (JSON tool output only)
cache-hintsRequest prompt cache hints (normally on by default)
routeRequest model routing
style=cavemanAdd terse output shaping instruction
style=conciseAdd mild output shaping instruction
style=offDo not add output shaping for this request

SDK per-request control#

The TypeScript and Python SDKs translate options into the same header bytes.

// Pass this request byte-identical
await cave.openai().responses.create(body, { cave: { optimize: "off" } });

// Disable cache hints and compression for this request
await cave.openai().responses.create(body, {
cave: { optimize: { cacheHints: false, compress: false } },
});

// Request compression and a style for this request
await cave.openai().responses.create(body, {
cave: { optimize: { compress: true, style: "caveman" } },
});

Response receipts and headers#

Every response carries disclosure headers describing what happened. Read them to distinguish applied and denied optimizations.

HeaderMeaning
x-cave-optimize-appliedComma list of opt-ups that ran (preconditions cleared)
x-cave-optimize-deniedtoken=reason pairs for refused opt-ups
x-cave-cache-modeThe response cache mode applied (respect, force, semantic, disable)
x-cave-response-cacheCache outcome: hit, hit_semantic, shadow_hit, shadow_miss, miss, bypass, uncacheable
x-cave-cache-statusThe provider's prompt cache outcome (hit, miss, write, partial, unknown)

Parse receipts with the SDK#

import { parseReceipt } from "@caveman-ai/sdk";

const response = await cave.openai().raw("/v1/responses", {
method: "POST",
body,
});
const receipt = parseReceipt(response.headers);
// receipt.optimizeApplied, receipt.optimizeDenied, receipt.responseCache, etc.

A header the gateway did not send stays absent in the receipt. The receipt never fills in a default value.

Which optimizers exist#

Optimizers are grouped by safety class. The gateway only runs an optimizer when its preconditions are met and the request or project policy allows it.

S0/S1 byte-safe (no model-visible change)#

OptimizerProviderDefaultWhat it does
anthropic-cache-breakpointsAnthropicOnInjects ephemeral cache_control on the largest stable prefix so repeated prefixes bill at cache_read rate
openai-prompt-cache-keyOpenAIOnInjects a canonical cache key to improve routing affinity for OpenAI prompt caching
bedrock-cache-pointsBedrock (Anthropic Claude)OffAdds Bedrock-native cache marker to the stable prefix

S1 behavioral (can change output, eval-gated)#

OptimizerProviderDefaultWhat it does
output-brevityOpenAIOff, eval-gatedAdds a 512-token max_output_tokens cap when the caller sets none
reasoning-effortOpenAIOff, eval-gatedSets reasoning_effort: "low" on reasoning-capable models when the caller sets none

S2 structural (needs SDK cooperation)#

  • toon-reencoding sends x-cave-optimize: compress=lossless to rewrite JSON tool output to a shorter form that round-trips to the same data.

S3 behavioral (eval-gated rollout)#

  • cheaper-action-fit routes to a cheaper model that graded at least as well on replayed cases.
  • tool-output-size-profile and duplicate-step-profile identify structural patterns that require code changes in the agent.

Cache control per request#

x-cave-cache controls the response cache (the gateway's own stored answers, separate from provider prompt cache).

ValueBehavior
respect (default)Read cache if present, write on miss
forceServe exact match from cache, or store if miss
ttl=<seconds>Force with a custom TTL (clamped to 60 to 86400)
semanticServe semantic matches; shadow mode if plan does not allow
semantic,ttl=<seconds>Semantic with custom TTL
disableSkip cache read and write
await cave.openai().responses.create(body, {
cave: { cache: { mode: "force", ttlSeconds: 900 } },
});

Opting out per request#

Use opt-down tokens when one request must not touch a specific optimization:

curl "${CAVE_GATEWAY_URL}/openai/v1/chat/completions" \
-H "authorization: Bearer ${CAVE_API_KEY}" \
-H "x-cave-optimize: no-cache,no-compress" \
-H "content-type: application/json" \
-d '{"model":"gpt-4o","messages":[{"role":"user","content":"hi"}]}'

What compression is measured at#

The compression optimizer measured 33.2% fewer provider-reported input tokens on the wrap benchmark (95% CI 14.6% to 48.5%). That is a token-count reduction observed on one benchmark corpus, not a promise about your traffic and not a dollar figure. Gateway reporting stays labeled inferred until a verified method backs it.

Next steps#

Esc

Start here