Send less context.
Keep control.
TrimLayer returns the smallest safe context for your application. You choose if, when and where that context is sent next; TrimLayer does not call a model provider on your behalf.
optimized_context in both cases.Choose an integration
TrimLayer is an optimize-only service: it returns a verified context, while your application makes any later model request directly.
You call the model
Best for provider independence, custom orchestration, local models, RAG pipelines or strict network boundaries.
- Only a TrimLayer key is sent to us
- No provider credential is accepted
- No LLM call is made by TrimLayer
- You decide where to use the result
Optimize onlyYou own the final model call.Optimize onlyThe endpoint rejects provider credentials.RecoveryYour application decides whether to retrieve it.Create a project key
Open Console → API keys, create a key and store the displayed secret immediately. It begins with cl_ and is shown only once. Choose an expiry that matches the environment.
TRIMLAYER_KEY=cl_your_project_key
TRIMLAYER_BASE_URL=https://api.trimlayer.comCompiler quickstart
The compiler returns context for your application to use elsewhere. A task or query improves focused selection but is optional. Use domain: auto unless your application has a reliable domain declaration.
/v1/context/optimizeRaw HTTP
curl https://api.trimlayer.com/v1/context/optimize \
-H "X-TrimLayer-Key: $TRIMLAYER_KEY" \
-H "Content-Type: application/json" \
-d '{
"content": "Your long document, prompt or retrieved context...",
"query": "Which cancellation conditions apply?",
"domain": "auto"
}'TypeScript
import { TrimLayerCompiler } from "@trimlayer/openai";
const compiler = new TrimLayerCompiler({
baseURL: "https://api.trimlayer.com",
trimLayerKey: process.env.TRIMLAYER_KEY!
});
const result = await compiler.optimize(longDocument, {
query: "Which cancellation conditions apply?", // optional
domain: "auto"
});
// Always use this field. It contains the accepted candidate or,
// when protected, the byte-identical original.
const contextForYourLLM = result.optimized_context;Python
from trimlayer_openai import TrimLayerCompiler
compiler = TrimLayerCompiler(
base_url="https://api.trimlayer.com",
trimlayer_key=os.environ["TRIMLAYER_KEY"],
)
result = compiler.optimize(
long_document,
query="Which cancellation conditions apply?", # optional
domain="auto",
)
context_for_your_llm = result["optimized_context"]contentYesSource to optimize. UTF-8, up to 4 MiB.queryNoThe task the final model must answer.domainNoauto, general, health, legal, scientific or ambiguous_multihop.Authorization, X-Api-Key and X-Goog-Api-Key. It cannot spend provider credits.Understand the response
Use optimized_context as the downstream context. Other fields explain the decision and measure savings without storing prompt content.
{
"decision": "optimized",
"optimized_context": "The accepted, compact context...",
"original_tokens": 9341,
"optimized_tokens": 3641,
"avoided_tokens": 5700,
"reduction_percent": 61.0,
"counting_method": "o200k_base",
"recovery_available": true,
"recovery_id": "clctx_0123456789abcdef0123456789abcdef",
"latency_ms": 24.8,
"verification_level": "balanced",
"proof": {
"exact_recoverable": true,
"format": "text",
"query_parts": 4,
"query_parts_covered": 4
}
}decisionoptimized means a candidate passed. protected means the original was returned.
optimized_contextThe safe value to send downstream in either state.
avoided_tokensOriginal measured tokens minus dispatched context tokens.
reduction_percentInitial reduction before any later recovery call.
verification_levelThe safety profile that produced the result. Keep application validation for consequential decisions.
proofContent-free evidence about format, coverage, repair and recoverability.
recovery_idA scoped, short-lived reference to the exact original.
Focused questions
A focused query tells the compiler which evidence is necessary. Keep the source in content and the task in query; do not concatenate them when your integration can keep them separate.
{
"content": contractText,
"query": "What notice and cure periods apply?",
"domain": "auto"
}Conditions, exceptions and required support can be selected together.
{
"content": contractText + "\n\nSummarize everything",
"domain": "auto"
}A broad task can require most of the document and leave little safe reduction.
Leave query out for generic compaction. Expect a more conservative result because material may matter to an unknown future task.
Improve savings without losing the answer
TrimLayer reduces the context that is unnecessary for a specific task. The clearest way to improve a result is to state the decision, fact, exception or code path the model needs—not to relax safety settings.
Name one outcome
Ask for a decision, a deadline, an exception or an implementation path. Broad summaries often require the entire document.
What notice and cure periods apply?Keep the task separate
Send source in content and the goal in query. This lets the compiler distinguish evidence from instructions.
content: contractText
query: "Who may terminate?"Use the narrowest source
For repositories, pass a file path through MCP and name the function or type. For RAG, send retrieved passages rather than the full corpus.
Explain CreateAPIKey persistence.Architecture, security, quota, provider and operations evidence may all be necessary. A protected result is likely—and correct.
The compiler can focus on the key hash, tenant boundary and transaction evidence while leaving unrelated modules out.
domain: auto by defaultDeclare a domain only when your application already knows it reliably.effective reduction when recovery happens. Initial savings are valuable only if later evidence retrieval does not erase them.Content behavior
TrimLayer does not apply one lossy rewrite to every input. It identifies the content shape, protects structures that must remain exact and returns the original whenever an eligible candidate cannot be proven useful.
Plain textFocused selection with a query; conservative compaction without one.Provide a specific task when only part of a long document is needed.JSON / tool outputRecognized structure and critical values remain protected.Send valid JSON and keep downstream schema validation enabled.CodeCode blocks remain intact while eligible surrounding explanation may shrink.Run tests or static checks; optimization is not code verification.Numbers and datesDetected values, units, ranges and linked conditions receive extra protection.Use deterministic validation for consequential calculations.Images and filesThe optimize API accepts text, not binary uploads.Extract the relevant text before optimization; file handling stays in your application.decision: protected. This is expected for short, dense, ambiguous or already-efficient content.Exact recovery
When a result contains a recovery ID, your application can request the exact original source. The recovered bytes are not a summary or regenerated text.
/v1/context/recover/:idconst exactOriginal = await compiler.recover(result.recovery_id!);
// Your application decides whether to send it in a follow-up turn.Usage and quota
Completed requests produce content-free measurement metadata. The dashboard shows original tokens, dispatched tokens, avoided tokens, decision, latency, recovery cost and effective reduction.
Every optimize-only API or MCP call reserves quota using the measured original source tokens, including protected results. Free includes 200K input tokens/month at 6 requests or 30K input tokens per minute; Starter includes 5M at 20 requests or 200K input tokens per minute; Growth includes 50M at 60 requests or 600K input tokens per minute. The dashboard’s authenticated verification flow is reported separately as Test traffic; project keys cannot select that lane.
/v1/context/requests/:request_idErrors and safe outcomes
HTTP errors mean processing failed. A 200 response with decision: protected means processing succeeded and the original is safe to use.
invalid_optimize_requestCheck JSON fields, domain and UTF-8.provider_credentials_not_acceptedRemove provider credentials from compiler calls.invalid_trimlayer_keyUse an active project key.request_too_largeSplit the source into bounded documents.quota_exceededWait for renewal or upgrade the plan.rate_limit_exceededHonor Retry-After; Free and Growth have different throughput limits.gateway_busyRetry with backoff and jitter.Security boundaries
Credentials
Project keys are one-way digests. Provider credentials are neither requested nor accepted by the optimize API.
Content
Analytics is content-free. Recovery originals are tenant-scoped, encrypted and expire.
Network
The optimize API accepts source text and a TrimLayer project key only; no downstream model endpoint is contacted.
Fallback
Unknown structures and failed verification return the original.
- Keep secrets server-side and redact sensitive headers from logs.
- Use separate keys and environments for development, staging and production.
- Scope project keys to the smallest required environment and project.
- Use application-owned validation for consequential legal, health and financial outputs.
- Rotate a key immediately when exposure is suspected.
Production checklist
MCP integration
Use the local trimlayer-mcp server in Codex, Claude Code, Claude Desktop, Cursor or any stdio MCP host. It prepares context before model use; it does not call a model provider and never needs an OpenAI, Anthropic or Google key.
make build-mcp once per release.optimized_context, or retrieve one named omitted detail.Codex: one shared setup
Add this to ~/.codex/config.toml for your account, or .codex/config.toml for one trusted repository. Codex Desktop, CLI and the IDE extension share this MCP configuration.
[mcp_servers.trimlayer]
command = "/ABSOLUTE/PATH/ai-optimize/bin/trimlayer-mcp"
startup_timeout_sec = 20
tool_timeout_sec = 120
[mcp_servers.trimlayer.env]
TRIMLAYER_BASE_URL = "https://api.trimlayer.com"
TRIMLAYER_API_KEY = "cl_REPLACE_WITH_PROJECT_KEY"
TRIMLAYER_MCP_TIMEOUT = "2m"
TRIMLAYER_ALLOWED_ROOTS = "/ABSOLUTE/PATH/TO/WORKSPACE:/ABSOLUTE/PATH/TO/DOCUMENTS"Restart Codex, then use /mcp in the terminal UI to verify that trimlayer is connected.
Ask the host to use it first
Before reading the source, call trimlayer_optimize_file for:
/absolute/path/to/repository/internal/gateway/optimize.go
Task: Explain validation, quota reservation and recovery behavior.
Use only optimized_context for the first answer. If decision is protected,
use optimized_context unchanged. Retrieve only a named missing detail.Use trimlayer_optimize_file for a local path. The host model receives only the accepted context, not the entire source file.
If someone already pasted the full document in a normal chat turn, the host may have spent those initial input tokens. Use a file, dashboard or HTTP pre-send integration instead.
Claude, Cursor and other MCP hosts
Choose a local stdio MCP server in the client’s settings, set the command to bin/trimlayer-mcp, then copy the same environment variables. Keep TRIMLAYER_ALLOWED_ROOTS limited to folders the tool may read. On macOS and Linux, separate multiple roots with :; Windows uses ;.
For large context, optimize before reading it into the model.
Prefer trimlayer_optimize_file when a local path exists.
Pass the user's task as query and leave domain=auto unless known.
Use mode=balanced by default. Treat optimized_context as the source of truth.
If decision=protected, do not retry with weaker settings.
Use trimlayer_retrieve only for a specific omitted detail.make build-mcp
TRIMLAYER_BASE_URL=http://127.0.0.1:8080 \
TRIMLAYER_API_KEY=$TRIMLAYER_KEY \
TRIMLAYER_ALLOWED_ROOTS=/absolute/path/to/documents \
./bin/trimlayer-mcpAvailable tools: trimlayer_optimize_text, trimlayer_optimize_file, trimlayer_retrieve and trimlayer_gateway_status.