| 1 | # Context Window Plugin DOX |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | - Own context-window token accounting, the usage API, the composer indicator, |
| 6 | its popover, and its Interface visibility row. |
| 7 | |
| 8 | ## Ownership |
| 9 | |
| 10 | - `helpers/usage.py` owns per-prompt bucket measurement and reconciliation. |
| 11 | - `extensions/python/` records prompt parts at their source extension points, |
| 12 | preserves terminal streamed usage, and captures optional provider usage. |
| 13 | - `api/context_window.py` exposes the active chat's token usage and effective |
| 14 | model limit without returning prompt content. |
| 15 | - `webui/` and `extensions/webui/` own the Alpine store, indicator, popover, |
| 16 | model-override refresh, and Interface visibility row. |
| 17 | |
| 18 | ## Local Contracts |
| 19 | |
| 20 | - The six used-token buckets are `messages`, `system_tools`, `skills`, |
| 21 | `mcp_tools`, `system_prompt`, and `extras`. |
| 22 | - Tools, MCP tools, and the available-skills catalog are measured from their |
| 23 | extensible prompt builders, never inferred from rendered headings. |
| 24 | - Loaded skill instructions are removed from Messages and added to Skills. |
| 25 | - Protocol and prompt extras are reported together as Extras. |
| 26 | - Messages reuse the history record token ledger; independently rendered |
| 27 | fragments use a bounded, content-addressed, runtime-only cache. |
| 28 | - Bucket totals reconcile to the already-stored prompt token total; the |
| 29 | unclaimed remainder belongs to System prompt. |
| 30 | - If the history ledger would consume the whole prompt estimate, recompute only |
| 31 | the rendered message portion before reconciliation; ordinary prompt builds |
| 32 | keep the fast ledger path. |
| 33 | - The prompt estimate never guesses provider-specific image token costs or |
| 34 | counts embedded image bytes as text. |
| 35 | - Provider price, cache hit, and input/output tokens form a flat summary without |
| 36 | diagnostic detail rows. |
| 37 | - Provider rows are exposed only when the provider or transport reports their |
| 38 | values; unavailable price and cache data render no row. |
| 39 | - Streamed OpenRouter main turns request LiteLLM's terminal usage event. The |
| 40 | response callback still runs normally; only an actual Chat Completions result |
| 41 | restores the accepted response after the accounting tail is drained. |
| 42 | - Responses API turns keep their native result and callback behavior unchanged. |
| 43 | - Older chats without a stored breakdown show the explanatory empty state. |
| 44 | - The indicator refreshes once per new Agent 0 generation and again when the |
| 45 | run completes; streamed updates to the same generation do not refetch it. |
| 46 | - `_model_config` supplies the effective model limit and the |
| 47 | `model-context-strip-end` WebUI slot; it does not own this feature's state. |
| 48 | - The `contextWindowUsage` Interface setting defaults to visible on mobile and |
| 49 | desktop. |
| 50 | |
| 51 | ## Work Guidance |
| 52 | |
| 53 | - Keep prompt accounting out of rendered-text heuristics. |
| 54 | - Keep provider-reported usage separate from the six estimated context buckets. |
| 55 | - Keep the API response limited to counts needed by the UI. |
| 56 | - Preserve the upward, right-aligned popover geometry used beside the model and |
| 57 | profile selectors. |
| 58 | |
| 59 | ## Verification |
| 60 | |
| 61 | - Run `conda run -n a0 pytest plugins/_context_window/tests`. |
| 62 | - Smoke-test the indicator, popover, chat switching, post-run refresh, and |
| 63 | mobile/desktop visibility against the live WebUI. |
| 64 | |
| 65 | ## Child DOX Index |
| 66 | |
| 67 | No child DOX files. |