CosavuCosavu

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/context

Optimize

POST/v1/context/optimize

Compress a prompt before sending to your LLM. Returns the optimised prompt along with token savings and the parsed PromptIR.

Request body

ParamTypeDescription
promptstringThe raw prompt to optimise. Required.
budgetnumberMaximum tokens to return. Optional, defaults to no limit.
preservePIIbooleanSkip 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/parse

Parse 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

TypeDescription
IDENTITYPersona or role assignment for the model.
INSTRUCTIONWhat the model should do.
CONTEXTBackground information or retrieved documents.
CONSTRAINTOutput format, length, or behavioural rules.
EXAMPLEFew-shot examples.
OUTPUT_FORMATSchema or template for the response.

See also

For error response shapes, status codes, and retry logic, see Errors.