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.
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.
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" } },
});# Pass this request byte-identical
cave.openai().responses.create(body, optimize="off")
# Disable cache hints and compression
cave.openai().responses.create(
body, optimize={"cache_hints": False, "compress": False}
)
# Request compression and style
cave.openai().responses.create(
body, 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.
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.from caveman_cloud import parse_receipt
receipt = parse_receipt(response.headers)
# Receipt(mode, optimizations, cache_status, request_id,
# compression_ratio, tokens_before, tokens_after, recovery_handle)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)#
S1 behavioral (can change output, eval-gated)#
S2 structural (needs SDK cooperation)#
toon-reencodingsendsx-cave-optimize: compress=losslessto rewrite JSON tool output to a shorter form that round-trips to the same data.
S3 behavioral (eval-gated rollout)#
cheaper-action-fitroutes to a cheaper model that graded at least as well on replayed cases.tool-output-size-profileandduplicate-step-profileidentify 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).
await cave.openai().responses.create(body, {
cave: { cache: { mode: "force", ttlSeconds: 900 } },
});cave.openai().responses.create(
body, cache={"mode": "force", "ttl_seconds": 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.