Skip to main content

lean_ctx/core/
context_overhead.rs

1//! Honest accounting of the fixed per-turn context lean-ctx injects (GitHub #361).
2//!
3//! Three components ride every request and — on a provider WITHOUT prompt caching
4//! — are re-billed on every turn:
5//!  - the exposed MCP **tool schemas** (description + input schema of each tool),
6//!  - the MCP **server instructions** block, and
7//!  - the **rules block** lean-ctx writes into the host's instruction file
8//!    (`CLAUDE.md` / `AGENTS.md`).
9//!
10//! `lean-ctx gain` measures *compression on lean-ctx-touched reads* — its
11//! denominator is lean-ctx traffic, not the provider bill. On a phase-isolated /
12//! non-caching workload (separate process per phase, no provider cache) the
13//! cached-re-read lever has no surface, so the headline can read net-positive
14//! while the bill moved net-negative. Surfacing this overhead — and stating the
15//! denominator — keeps the meter honest.
16//!
17//! Net bill impact ≈ `gross_saved_tokens − total_tokens() × turns`.
18
19use std::sync::OnceLock;
20
21use crate::core::tokens::count_tokens;
22
23/// A measured breakdown, in tokens, of the per-turn context lean-ctx adds.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub struct ContextOverhead {
26    /// Number of MCP tools exposed (the schema-bearing surface).
27    pub tool_count: usize,
28    /// Tokens for all exposed tool descriptions + input schemas.
29    pub tool_schema_tokens: usize,
30    /// Tokens for the MCP server instructions block (capped at the instruction budget).
31    pub instruction_tokens: usize,
32    /// Tokens for the rules block injected into the host instruction file.
33    pub rules_block_tokens: usize,
34}
35
36impl ContextOverhead {
37    /// Total per-turn overhead in tokens.
38    #[must_use]
39    pub fn total_tokens(&self) -> usize {
40        self.tool_schema_tokens + self.instruction_tokens + self.rules_block_tokens
41    }
42
43    /// Process-cached overhead. The tool surface and rules block are static and
44    /// the instruction block varies only with slow-moving session state, so a
45    /// once-per-process measurement is the right tradeoff for callers that render
46    /// repeatedly (the `gain` dashboard re-renders every second in `--live`) —
47    /// it avoids per-tick disk I/O and re-tokenization.
48    #[must_use]
49    pub fn cached() -> Self {
50        static CACHE: OnceLock<ContextOverhead> = OnceLock::new();
51        *CACHE.get_or_init(Self::measure)
52    }
53
54    /// Measure the overhead for the currently-configured MCP surface. Uses the
55    /// same advertisement policy as the live `tools/list` handler (candidate
56    /// set, profile gates, invoker, description compression), so the number
57    /// reflects what this install actually advertises (#572).
58    #[must_use]
59    pub fn measure() -> Self {
60        let tools = crate::server::tool_visibility::advertised_tool_defs_default();
61        let tool_count = tools.len();
62        let tool_schema_tokens = tools.iter().map(tool_tokens).sum();
63
64        let instructions =
65            crate::instructions::build_instructions(crate::tools::CrpMode::effective());
66        let instruction_tokens = count_tokens(&instructions);
67
68        let rules_block_tokens = count_tokens(crate::rules_inject::canonical_rules_block());
69
70        Self {
71            tool_count,
72            tool_schema_tokens,
73            instruction_tokens,
74            rules_block_tokens,
75        }
76    }
77}
78
79/// Description + input-schema tokens for one tool definition — exactly the two
80/// fields a client re-sends in every request's tool list.
81pub fn tool_tokens(t: &rmcp::model::Tool) -> usize {
82    let desc = t
83        .description
84        .as_ref()
85        .map_or(0, |d| count_tokens(d.as_ref()));
86    let schema = count_tokens(&serde_json::to_string(&t.input_schema).unwrap_or_default());
87    desc + schema
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn measure_reports_nonzero_components() {
96        let o = ContextOverhead::measure();
97        assert!(o.tool_count > 0, "must expose at least one tool");
98        assert!(o.tool_schema_tokens > 0, "tool schemas carry tokens");
99        assert!(o.instruction_tokens > 0, "instructions carry tokens");
100        assert!(o.rules_block_tokens > 0, "rules block carries tokens");
101        assert_eq!(
102            o.total_tokens(),
103            o.tool_schema_tokens + o.instruction_tokens + o.rules_block_tokens
104        );
105    }
106
107    #[test]
108    fn total_is_sum_of_parts() {
109        let o = ContextOverhead {
110            tool_count: 10,
111            tool_schema_tokens: 100,
112            instruction_tokens: 200,
113            rules_block_tokens: 50,
114        };
115        assert_eq!(o.total_tokens(), 350);
116    }
117}