Contextwaise

Contextwaise is in early access: API keys and the dashboard open at launch. Join the waitlist to get yours first.

Docs · Optimize context›
API reference

Optimize context

Send a question and the context you would give your model. Get back the context worth sending, and a report on every decision.

Endpoint#

POST/api/v1/optimize

Authenticated with a bearer key (Authentication). The request and response are JSON.

Request body#

querystringrequired

The question your model will answer, usually the user's message. Candidates are scored against it. Up to 8,000 characters.

candidatesarrayrequired

The context items to choose from, up to 500. See Candidates.

agentstringoptionalDefault: default

A stable name for the call site, such as support-assistant. Groups requests in the dashboard, selects the agent's budget, and scopes the quality check's tuning.

modelstringoptional

The model id you send this context to, like gpt-6.1-sol or claude-sonnet-5-5. Costs in the report are priced for it. Unknown models fall back to your project's default model; report.model shows which was used.

conversationstringoptional

Turns on cache-safe mode: items seen earlier in this conversation keep their first decision. 1 to 128 characters: letters, digits, _ - . :. See Conversations and prompt caching.

Candidates#

Each candidate is either an object or a plain string:

textstringrequired

The content, up to 60,000 characters. HTML, JSON, logs and test output are cleaned automatically.

idstringoptionalDefault: c0, c1, …

Your identifier, returned in context and report.items. Use your document or chunk id. Up to 80 characters.

sourcestringoptionalDefault: context

A label for where it came from. Any name you choose works, such as rag, web, history or your own intern_data. Used to break savings down by source and to filter requests in the dashboard. It doesn't change scoring.

Stored lowercase, up to 40 characters. Characters other than letters, digits, _, - and spaces become -, so Intern.Data is stored as intern-data.

Tuning#

budgetintegeroptionalDefault: agent budget, then project default

The most tokens of context that can come back, from 200 to 200,000 (values outside are clamped). Strongest items are kept first. Defaults to the agent's budget set in the dashboard, otherwise the project default (6,000).

aggressivenessstringoptionalDefault: project setting (balanced)

conservative, balanced or aggressive: the base relevance threshold (0.9, 1.4, 1.9 on the 0 to 3 scale). See Selection.

Model routing#

Optional. Sending models turns on routing advice in report.route. See Model routing.

modelsstring[]optional

2 to 10 model ids you approve, from any providers. Each must be in the catalog or priced in Settings → Models, otherwise the request returns 422.

systemstringoptional

Your model's instructions. Helps judge how demanding the task is.

outputTokensintegeroptionalDefault: 500

Your typical answer length, to price output in the switch estimate.

promptTokensintegeroptional

Your full prompt size including history, for example the last usage.input_tokens. Defaults to what Contextwaise sees.

Response#

idstringrequired

The request id, as shown in the dashboard (req_…).

contextarrayrequired

The kept candidates as { id, text, source }, in the order you sent them. Send these to your model and store them in the conversation. text is the cleaned version.

reportobjectrequired

Every figure and decision behind the result, described below.

The report#

tokensobjectrequired

before, after, saved and savedPct: input tokens of the context as received and as returned.

costobjectrequired

before and after at the model's input price, scorer (the cost of relevance scoring) and avoided (what you save, net of scoring). In USD.

modelstring | nullrequired

The model costs were priced for. null when your project's custom price was used.

inputPricePerMillionnumberrequired

The input price used, in USD per 1M tokens.

itemsarrayrequired

One entry per candidate. See Items.

budget · aggressiveness · thresholdnumber · string · numberrequired

The settings applied. threshold includes the agent's learned adjustment.

conversationobject | nullrequired

With a conversation id: { id, reused, protected }, the number of decisions reused, and whether they were safely stored.

routeobject | nullrequired

Routing advice when models was sent. See Read the advice.

guardnullrequired

The quality check runs after the response, so it is null here. Its verdict appears on the request in the dashboard.

latencyMs · timingsnumber · objectrequired

Total time in milliseconds from the request arriving, and the split by step: auth, project, score, select.

Items#

id · sourcestringrequired

The candidate's id and source.

keptbooleanrequired

Whether it is in context.

reasonstringrequired

relevant (kept), low_relevance (below the threshold), duplicate (repeats a kept item, see duplicateOf) or over_budget (relevant, but the budget was full).

scorenumberrequired

Relevance from 0 (unrelated) to 3 (essential).

tokens · originalTokensintegerrequired

The size after cleanup, and as received. They differ when cleanup removed noise.

cleanedstring[]optional

What cleanup removed, such as converted HTML to text or dropped 12 empty JSON fields. Present only when the item was cleaned.

duplicateOfstringoptional

For duplicates: the id of the kept item it repeats.

reusedbooleanoptional

In cache-safe mode: the decision was made earlier in the conversation.

Full example#

Request
{
  "query": "Why was I charged twice this month?",
  "candidates": [
    { "id": "doc-12", "text": "Duplicate charges happen when a card payment times out and is retried…", "source": "rag" },
    { "id": "doc-40", "text": "Our office hours are Monday to Friday, 9am to 6pm CET.", "source": "rag" }
  ],
  "agent": "support-assistant",
  "model": "gpt-6.1-sol"
}
curl https://app.contextwaise.com/api/v1/optimize \
  -H "Authorization: Bearer $CONTEXTWAISE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "query": "Why was I charged twice this month?",
  "candidates": [
    { "id": "doc-12", "text": "Duplicate charges happen when a card payment times out and is retried…", "source": "rag" },
    { "id": "doc-40", "text": "Our office hours are Monday to Friday, 9am to 6pm CET.", "source": "rag" }
  ],
  "agent": "support-assistant",
  "model": "gpt-6.1-sol"
}'
Response · 200 OK
{
  "id": "req_8Hc2kQ0aZt1x",
  "context": [
    { "id": "doc-12", "text": "Duplicate charges happen when a card payment times out and is retried…", "source": "rag" }
  ],
  "report": {
    "tokens": { "before": 9120, "after": 2310, "saved": 6810, "savedPct": 74.67 },
    "cost": { "before": 0.0182, "after": 0.0046, "scorer": 0.0004, "avoided": 0.0132 },
    "model": "gpt-6.1-sol",
    "inputPricePerMillion": 2,
    "items": [
      { "id": "doc-12", "source": "rag", "tokens": 120, "originalTokens": 120, "score": 2.8, "kept": true, "reason": "relevant" },
      { "id": "doc-40", "source": "rag", "tokens": 14, "originalTokens": 14, "score": 0, "kept": false, "reason": "low_relevance" }
    ],
    "guard": null,
    "budget": 6000,
    "aggressiveness": "balanced",
    "threshold": 1.4,
    "conversation": null,
    "route": null,
    "latencyMs": 180,
    "timings": { "auth": 3, "project": 2, "score": 170, "select": 1 }
  }
}