ContextAPI · Reference
ContextAPI
ContextAPI sits in front of any LLM and returns a token-efficient version of your prompt. Two endpoints — optimize and parse — cover most workflows.
Base URL
All ContextAPI requests use the following base URL:
https://api.cosavu.com/v1/contextOptimize
POST
/v1/context/optimizeCompress a prompt before sending to your LLM. Returns the optimised prompt along with token savings and the parsed PromptIR.
Request body
| Param | Type | Description |
|---|---|---|
prompt | string | The raw prompt to optimise. Required. |
budget | number | Maximum tokens to return. Optional, defaults to no limit. |
preservePII | boolean | Skip the PII scrubbing pass. Defaults to false. |
mode | 'auto' | 'aggressive' | 'light' | Override the STAN-1-Mini policy. Defaults to 'auto'. |
Example
const result = await cosavu.context.optimize({ prompt: "Could you please kindly explain RAG in detail", budget: 512, mode: "auto", }) console.log(result.optimizedPrompt) console.log(result.tokensSaved)
Response
{
"color:#3b82f6">"optimizedPrompt": "Explain RAG in detail.",
"color:#3b82f6">"originalTokens": 84,
"color:#3b82f6">"outputTokens": 32,
"color:#3b82f6">"tokensSaved": 52,
"color:#3b82f6">"compressionPct": 0.619,
"color:#3b82f6">"latencyMs": 12,
"color:#3b82f6">"ir": {
"color:#3b82f6">"blocks": [
{ "color:#3b82f6">"blockType": "INSTRUCTION", ">"tokens": 32, ">"isCompressed": true }
],
"color:#3b82f6">"messinessScore": 0.61
}
}Parse
POST
/v1/context/parseParse a prompt into its typed PromptIR blocks without optimising. Useful when you want to inspect prompt structure or build custom optimisation logic on top.
Example
const ir = await cosavu.context.parse({ prompt: "You are an expert. Given these docs, answer concisely.", }) for (const block of ir.blocks) { console.log(block.blockType, block.tokens) }
Block types
| Type | Description |
|---|---|
IDENTITY | Persona or role assignment for the model. |
INSTRUCTION | What the model should do. |
CONTEXT | Background information or retrieved documents. |
CONSTRAINT | Output format, length, or behavioural rules. |
EXAMPLE | Few-shot examples. |
OUTPUT_FORMAT | Schema or template for the response. |
See also
For error response shapes, status codes, and retry logic, see Errors.