# Context Window Optimizer – Chunk Ranking & Selection

> Context Window Optimizer – Chunk Ranking & Selection is a paid API for AI agents from api.strale.io, paid per call via x402, $0.0324/call, status unknown (last checked 2026-10-12).

Ranks and selects document chunks by relevance to a query to fit within a specified token budget, returning each chunk with a relevance score and inclusion/exclusion status.

## Facts

- Endpoint: GET https://api.strale.io/x402/context-window-optimize?utm_source=zero.xyz
- Price: $0.0324/call
- Payment: x402
- Status: unknown
- Last checked: 2026-10-12
- Activations on Zero: 0
- Tags: x402
- Canonical page: https://www.zero.xyz/c/api-strale-io-b2f9a116
- Structured record (JSON): https://api.zero.xyz/v1/capabilities/cap_U0MiTC4Vc4URuqUfcTX3B

Status and success rate cover calls made through Zero and Zero's own probes. Third-party monitors may report differently.

## How to call it through Zero

Zero handles the 402 payment challenge and records the run. With the Zero CLI installed (`npm i -g @zeroxyz/cli`):

```sh
zero fetch --capability api-strale-io-b2f9a116
```

Example prompt: I have these 12 document chunks retrieved for the query 'how does photosynthesis work' — can you rank them by relevance and select only the ones that fit within 2000 tokens, so I know which to include in my LLM prompt?

## When to prefer this

Use this endpoint when building RAG (retrieval-augmented generation) pipelines where you have more retrieved chunks than can fit in an LLM's context window and need a principled, scored way to select the most relevant ones. Prefer this over naive truncation or embedding-similarity-only approaches when you need both a relevance ranking and automatic budget-aware selection in a single API call.

## Known failure modes

- Missing required 'documents' or 'query' query parameter returns a 400 validation error
- Empty documents array may return an empty selection with no chunks scored
- max_tokens set too low to include any chunk returns all chunks as excluded
- Overly large documents array may hit request size limits
- Payment not provided or insufficient USDC results in x402 payment required response
- Malformed document objects missing 'id' or 'text' fields cause schema validation failure

## How this service works

Rank and select document chunks by relevance to fit within a token budget. Returns selected/excluded chunks with scores.

## Output

Returns a list of document chunks each annotated with a relevance score, plus two groups: selected chunks (those that fit within the token budget ranked by relevance) and excluded chunks (those that didn't make the cut), allowing the agent to know exactly which content to pass into an LLM context window.

## Example request

```json
{
 "query": "how does photosynthesis work",
 "documents": [
  {
   "id": "doc1",
   "text": "Photosynthesis is a process used by plants and other organisms to convert light energy into chemical energy that can be stored as glucose. This process occurs primarily in the leaves of plants."
  },
  {
   "id": "doc2",
   "text": "The light-dependent reactions occur in the thylakoid membranes of chloroplasts and involve the absorption of photons by chlorophyll molecules."
  },
  {
   "id": "doc3",
   "text": "The Calvin cycle, also known as the light-independent reactions, takes place in the stroma and produces glucose from carbon dioxide."
  },
  {
   "id": "doc4",
   "text": "Chlorophyll is the primary pigment responsible for absorbing light energy in the visible spectrum, particularly in the blue and red wavelengths."
  }
 ],
 "max_tokens": 500
}
```

## Request schema (JSON Schema)

```json
{
 "type": "object",
 "$schema": "https://json-schema.org/draft/2020-12/schema",
 "required": [
  "input"
 ],
 "properties": {
  "input": {
   "type": "object",
   "required": [
    "type",
    "method"
   ],
   "properties": {
    "type": {
     "type": "string",
     "const": "http"
    },
    "method": {
     "enum": [
      "GET",
      "HEAD",
      "DELETE"
     ],
     "type": "string"
    },
    "queryParams": {
     "type": "object",
     "required": [
      "documents",
      "query"
     ],
     "properties": {
      "query": {
       "type": "string"
      },
      "documents": {
       "type": "array",
       "description": "Array of {id, text}"
      },
      "max_tokens": {
       "type": "integer"
      }
     }
    }
   },
   "additionalProperties": false
  }
 }
}
```

## More

- Live health (JSON, refreshed every minute): https://www.zero.xyz/c/api-strale-io-b2f9a116/health.json
- [Zero catalog index](https://www.zero.xyz/llms.txt)
- [Other services from api.strale.io](https://www.zero.xyz/host/api.strale.io/llms.txt)
