{"uid":"cap_U0MiTC4Vc4URuqUfcTX3B","slug":"api-strale-io-b2f9a116","name":"Context Window Optimizer – Chunk Ranking & Selection","description":"Rank and select document chunks by relevance to fit within a token budget. Returns selected/excluded chunks with scores.","url":"https://api.strale.io/x402/context-window-optimize?utm_source=zero.xyz","method":"GET","headers":{},"bodySchema":{"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}}},"responseSchema":null,"example":{"request":{"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},"response":{"_meta":{"payment":{"method":"x402","price_usd":0.0324,"settlement_id":"0x6c9f09596cd76f19d8196f70204b3bc092613bdb680e6f1c6fce1ce36494b6be"},"capability":"context-window-optimize","latency_ms":2254,"provenance":{"source":"claude-haiku","fetched_at":"2026-06-13T04:23:38.034Z"}},"query":"how does photosynthesis work","excluded":[],"selected":[{"id":"doc1","token_count":49,"relevance_score":0.95},{"id":"doc2","token_count":36,"relevance_score":0.8},{"id":"doc3","token_count":33,"relevance_score":0.78},{"id":"doc4","token_count":36,"relevance_score":0.65}],"max_tokens":500,"selected_count":4,"total_documents":4,"tokens_remaining":346,"total_tokens_used":154}},"exampleRequest":{"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},"tags":["x402"],"displayCostAmount":"0.0324","displayCostAsset":"USDC","priceDynamic":false,"priceHint":null,"priceStatus":"priced","priceSource":"settled","requiresHandshake":false,"reviewCount":0,"rating":{"score":"0.00","successRate":"0.00","reviews":0,"stars":null,"state":"unrated"},"availabilityStatus":"unknown","priceObserved":null,"sessionDeposit":null,"pricing":{"kind":"static","summary":"$0.0324/call","primary":{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.0324","per":"call","confidence":"exact"},"accepted":[{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.0324","per":"call","confidence":"exact"}]},"paymentMethods":[{"uid":"pm_694DJ9oHbo83_-qdxFpdG","protocol":"x402","methodType":"crypto","chain":"base","mode":"charge","costAmount":"0.0324","costPer":"request","priority":0,"asset":"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913","unit":"request","depositMicros":null,"planRef":null}],"brandName":null,"brandSlug":null,"brandBaseUrl":null,"brandDocsUrl":null,"whatItDoes":"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.","exampleAgentPrompt":"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?","exampleUseCases":null,"resultDescription":"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.","failureModes":["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"],"whenToPreferThis":"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.","instructions":null,"reviewSummary":null,"reviewSummaryHighlights":null,"reviewSummaryConcerns":null,"reviewSummaryGeneratedAt":null,"activationCount":0,"lastUsedAt":null,"lastSuccessfullyRanAt":null,"lastHealthCheckAt":"2026-10-12T18:46:55.104Z","isFirstParty":false,"canonicalSlug":"api-strale-io-b2f9a116"}