Documentation · v1
Support Console
DOCUMENTATION / GET STARTED
Context optimization infrastructure

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.

Your sourceText · JSON · code · retrieved context
TrimLayerMeasure · optimize · verify
Your applicationUse the returned context wherever you choose
One stable output contractIf optimization is safe, you receive the accepted candidate. If it is not, you receive the original input unchanged. Read 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.

Optimize only

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
Start with the compiler
NeedUseWhy
Any LLM or local modelOptimize onlyYou own the final model call.
No provider key sharedOptimize onlyThe endpoint rejects provider credentials.
Exact original needed laterRecoveryYour 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.

1CreateName the project and choose an expiry.
2StoreUse a server-side secret manager.
3TestSeparate test and production traffic.
4RotateCreate the replacement before revoking.
.env
TRIMLAYER_KEY=cl_your_project_key
TRIMLAYER_BASE_URL=https://api.trimlayer.com
Server-side onlyNever expose a TrimLayer key in browser code, mobile bundles, logs, screenshots or analytics.

Compiler 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.

POST/v1/context/optimize

Raw HTTP

terminal
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

compiler.ts
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

compiler.py
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"]
FieldRequiredDescription
contentYesSource to optimize. UTF-8, up to 4 MiB.
queryNoThe task the final model must answer.
domainNoauto, general, health, legal, scientific or ambiguous_multihop.
No hidden model callThis endpoint rejects 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.

response.json
{
  "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
  }
}
decision

optimized means a candidate passed. protected means the original was returned.

optimized_context

The safe value to send downstream in either state.

avoided_tokens

Original measured tokens minus dispatched context tokens.

reduction_percent

Initial reduction before any later recovery call.

verification_level

The safety profile that produced the result. Keep application validation for consequential decisions.

proof

Content-free evidence about format, coverage, repair and recoverability.

recovery_id

A scoped, short-lived reference to the exact original.

Protected is successfulA protected response is not an error. The engine could not prove a useful reduction and intentionally returned the 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.

Recommended
request.json
{
  "content": contractText,
  "query": "What notice and cure periods apply?",
  "domain": "auto"
}

Conditions, exceptions and required support can be selected together.

Avoid
request.json
{
  "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.

01

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?
02

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?"
03

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.
Too broad“Explain the backend.”

Architecture, security, quota, provider and operations evidence may all be necessary. A protected result is likely—and correct.

Better“How are API keys stored and isolated?”

The compiler can focus on the key hash, tenant boundary and transaction evidence while leaving unrelated modules out.

Use domain: auto by defaultDeclare a domain only when your application already knows it reliably.
Send valid JSON and intact codeStructure helps the compiler preserve exact fields, schemas and source blocks.
Treat protected as a useful signalNarrow the task or split an unrelated source; do not weaken safety just to force a percentage.
Measure the right numberUse 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.

InputTypical behaviorApplication guidance
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.
Optimization is selectiveA valid request can still produce 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.

GET/v1/context/recover/:id
recovery.ts
const exactOriginal = await compiler.recover(result.recovery_id!);

// Your application decides whether to send it in a follow-up turn.
1Send optimized context
2Detect missing evidence
3Recover exact source
4Continue the answer
Measure effective savingsInitial reduction is not final saving when recovery occurs. Subtract optimized input and every recovery input from original input.

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.

Original10,000Measured source input
Optimized4,000Initial model context
Recovery+600Later exact evidence
Effective saved54%5,400 tokens avoided

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.

GET/v1/context/requests/:request_id

Errors and safe outcomes

HTTP errors mean processing failed. A 200 response with decision: protected means processing succeeded and the original is safe to use.

StatusCodeWhat to do
400invalid_optimize_requestCheck JSON fields, domain and UTF-8.
400provider_credentials_not_acceptedRemove provider credentials from compiler calls.
401invalid_trimlayer_keyUse an active project key.
413request_too_largeSplit the source into bounded documents.
429quota_exceededWait for renewal or upgrade the plan.
429rate_limit_exceededHonor Retry-After; Free and Growth have different throughput limits.
503gateway_busyRetry with backoff and jitter.
Retry carefullyRetry 503 and 504 with bounded exponential backoff. Do not automatically retry authentication, validation or quota errors.

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.

01BuildRun make build-mcp once per release.
02ConnectAdd the binary command and project key to your MCP client.
03Optimize firstGive the tool a path and task before the host reads a large file.
04Answer safelyUse 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.

config.toml
[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

agent prompt
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.
Best for measurable savings

Use trimlayer_optimize_file for a local path. The host model receives only the accepted context, not the entire source file.

Important limitation

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 ;.

recommended agent instruction
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.
1File or pre-send text
2Optimize with task
3Use accepted context
4Retrieve only if needed
terminal
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-mcp

Available tools: trimlayer_optimize_text, trimlayer_optimize_file, trimlayer_retrieve and trimlayer_gateway_status.

Files are allowlistedThe MCP server accepts only configured roots and rejects paths outside them. Keep the allowlist narrow.