From 9db2e05c0e4aff7e12cf1607b8b54acb5f6e4c47 Mon Sep 17 00:00:00 2001 From: hermes Date: Thu, 1 Oct 2026 21:12:17 +0000 Subject: [PATCH] hx prefix: token breakdown of system prompt parts, tool schemas, MCP index --- crates/hx-cli/src/main.rs | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index dfcfc37..f154dd9 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -55,6 +55,8 @@ enum Cmd { Logout { provider: Kind }, /// Token report for a session file (usage, cache hit rate, costliest tool results). Stats { session: std::path::PathBuf }, + /// Token size of the fixed request prefix (system prompt parts, tool schemas, MCP index). + Prefix, /// Connect to configured MCP servers and print tools + index/eager token cost. Mcp, /// Serve lens diagnostics + LSP navigation as an MCP server over stdio. @@ -284,6 +286,58 @@ async fn main() -> Result<()> { print!("{}", hx_core::stats::report(&Session::open(session)?)); return Ok(()); } + Some(Cmd::Prefix) => { + let cwd = std::env::current_dir()?; + let cfg = Config::load(&cwd)?; + let home = context::hx_home(); + let hub = Arc::new( + hx_mcp::Hub::start(hx_mcp::config::load(&home, &cwd).map_err(anyhow::Error::msg)?) + .await, + ); + let mgr = Arc::new(hx_lens::manager::Manager::new( + cwd.clone(), + hx_lens::manager::default_servers(), + )); + let mut reg = hx_tools::registry(); + hub.register(&mut reg); + reg.add(hx_lens::LspTool::new(mgr.clone())); + let full = context::system_prompt(cfg.system_prompt.as_deref(), &home, &cwd); + let base = cfg + .system_prompt + .as_deref() + .unwrap_or(context::DEFAULT_PROMPT); + let files: usize = context::context_files(&home, &cwd) + .iter() + .map(|f| f.1.len()) + .sum(); + let idx = hub.system_prompt(); + let skills = full.len().saturating_sub(base.len() + files); + let mut rows: Vec<(String, usize)> = vec![ + ("system: base prompt".into(), base.len()), + ("system: AGENTS.md/CLAUDE.md files".into(), files), + ("system: skills + cwd line".into(), skills), + ("system: MCP index".into(), idx.len()), + ]; + let mut specs = reg.specs(); + specs.sort_by_key(|t| { + std::cmp::Reverse(serde_json::to_string(t).map_or(0, |s| s.len())) + }); + for t in &specs { + rows.push(( + format!("tool: {}", t.name), + serde_json::to_string(t).map_or(0, |s| s.len()), + )); + } + let total: usize = rows.iter().map(|r| r.1).sum::() / 4; + for (k, n) in rows { + println!("{:>7} tok {k}", n / 4); + } + println!( + "{total:>7} tok total (bytes/4; skills row = remainder of the system prompt)" + ); + mgr.shutdown().await; + return Ok(()); + } Some(Cmd::Mcp) => { let cwd = std::env::current_dir()?; let hub = hx_mcp::Hub::start(