lean-ctx 3.6.6

Context Runtime for AI Agents with CCP. 51 MCP tools, 10 read modes, 60+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
use serde::Serialize;
use serde_json::Value;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Provider {
    Anthropic,
    OpenAi,
    Gemini,
}

#[derive(Debug, Clone, Serialize)]
pub struct RequestBreakdown {
    pub provider: Provider,
    pub model: String,
    pub system_prompt_tokens: usize,
    pub user_message_tokens: usize,
    pub assistant_message_tokens: usize,
    pub tool_definition_tokens: usize,
    pub tool_definition_count: usize,
    pub tool_result_tokens: usize,
    pub image_count: usize,
    pub total_input_tokens: usize,
    pub message_count: usize,
    #[serde(default)]
    pub rules_tokens: usize,
    #[serde(default)]
    pub skills_tokens: usize,
    #[serde(default)]
    pub mcp_config_tokens: usize,
    #[serde(default)]
    pub subagent_tokens: usize,
    #[serde(default)]
    pub summarized_conversation_tokens: usize,
    #[serde(default)]
    pub conversation_tokens: usize,
}

pub fn analyze_request(body: &Value, provider: Provider) -> RequestBreakdown {
    match provider {
        Provider::Anthropic => analyze_anthropic(body),
        Provider::OpenAi => analyze_openai(body),
        Provider::Gemini => analyze_gemini(body),
    }
}

/// IDE clients (Cursor, Copilot) often send routing IDs like "model-0", "model-4"
/// instead of real model names. We keep track of the last real model name per provider
/// and fall back to it when we see a generic routing ID.
fn normalize_model(raw: &str, provider: Provider) -> String {
    use std::sync::Mutex;
    static LAST_REAL: Mutex<[Option<String>; 3]> = Mutex::new([None, None, None]);

    let is_routing_id = raw.starts_with("model-") || raw == "unknown" || raw.is_empty();

    let idx = match provider {
        Provider::Anthropic => 0,
        Provider::OpenAi => 1,
        Provider::Gemini => 2,
    };

    if is_routing_id {
        if let Ok(guard) = LAST_REAL.lock() {
            if let Some(ref real) = guard[idx] {
                return real.clone();
            }
        }
        return raw.to_string();
    }

    if let Ok(mut guard) = LAST_REAL.lock() {
        guard[idx] = Some(raw.to_string());
    }
    raw.to_string()
}

fn analyze_anthropic(body: &Value) -> RequestBreakdown {
    let raw_model = body
        .get("model")
        .and_then(|m| m.as_str())
        .unwrap_or("unknown");
    let model = normalize_model(raw_model, Provider::Anthropic);

    let mut system_prompt_tokens = 0;
    let mut rules_tokens = 0;
    let mut skills_tokens = 0;
    let mut mcp_config_tokens = 0;

    match body.get("system") {
        Some(Value::String(s)) => {
            let sp = classify_system_prompt(s);
            system_prompt_tokens = sp.base;
            rules_tokens = sp.rules;
            skills_tokens = sp.skills;
            mcp_config_tokens = sp.mcp;
        }
        Some(Value::Array(arr)) => {
            for block in arr {
                let text = block.get("text").and_then(|t| t.as_str()).unwrap_or("");
                let sp = classify_system_prompt(text);
                system_prompt_tokens += sp.base;
                rules_tokens += sp.rules;
                skills_tokens += sp.skills;
                mcp_config_tokens += sp.mcp;
            }
        }
        _ => {}
    }

    let tool_definition_tokens = body
        .get("tools")
        .and_then(|t| t.as_array())
        .map_or(0, |arr| json_chars(arr) / 4);

    let tool_definition_count = body
        .get("tools")
        .and_then(|t| t.as_array())
        .map_or(0, Vec::len);

    let mut user_message_tokens = 0;
    let mut assistant_message_tokens = 0;
    let mut tool_result_tokens = 0;
    let mut image_count = 0;
    let mut message_count = 0;
    let mut subagent_tokens = 0;
    let mut summarized_conversation_tokens = 0;

    if let Some(messages) = body.get("messages").and_then(|m| m.as_array()) {
        message_count = messages.len();
        for msg in messages {
            let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
            let content_tokens = estimate_content_tokens(msg.get("content"));
            let has_images = count_images(msg.get("content"));
            image_count += has_images;

            match role {
                "user" => {
                    if has_tool_results(msg.get("content")) {
                        tool_result_tokens += content_tokens;
                    } else if is_summary_message(msg.get("content")) {
                        summarized_conversation_tokens += content_tokens;
                    } else if is_subagent_message(msg.get("content")) {
                        subagent_tokens += content_tokens;
                    } else {
                        user_message_tokens += content_tokens;
                    }
                }
                "assistant" => assistant_message_tokens += content_tokens,
                _ => user_message_tokens += content_tokens,
            }
        }
    }

    let conversation_tokens = user_message_tokens + assistant_message_tokens;

    let total_input_tokens = system_prompt_tokens
        + rules_tokens
        + skills_tokens
        + mcp_config_tokens
        + user_message_tokens
        + assistant_message_tokens
        + tool_definition_tokens
        + tool_result_tokens
        + subagent_tokens
        + summarized_conversation_tokens;

    RequestBreakdown {
        provider: Provider::Anthropic,
        model,
        system_prompt_tokens,
        user_message_tokens,
        assistant_message_tokens,
        tool_definition_tokens,
        tool_definition_count,
        tool_result_tokens,
        image_count,
        total_input_tokens,
        message_count,
        rules_tokens,
        skills_tokens,
        mcp_config_tokens,
        subagent_tokens,
        summarized_conversation_tokens,
        conversation_tokens,
    }
}

fn analyze_openai(body: &Value) -> RequestBreakdown {
    let raw_model = body
        .get("model")
        .and_then(|m| m.as_str())
        .unwrap_or("unknown");
    let model = normalize_model(raw_model, Provider::OpenAi);

    let mut system_prompt_tokens = 0;
    let mut rules_tokens = 0;
    let mut skills_tokens = 0;
    let mut mcp_config_tokens = 0;
    let mut user_message_tokens = 0;
    let mut assistant_message_tokens = 0;
    let mut tool_result_tokens = 0;
    let mut image_count = 0;
    let mut message_count = 0;
    let mut subagent_tokens = 0;
    let mut summarized_conversation_tokens = 0;

    if let Some(messages) = body.get("messages").and_then(|m| m.as_array()) {
        message_count = messages.len();
        for msg in messages {
            let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
            let content_tokens = estimate_content_tokens(msg.get("content"));
            image_count += count_images(msg.get("content"));

            match role {
                "system" | "developer" => {
                    let text = extract_text_content(msg.get("content"));
                    let sp = classify_system_prompt(&text);
                    system_prompt_tokens += sp.base;
                    rules_tokens += sp.rules;
                    skills_tokens += sp.skills;
                    mcp_config_tokens += sp.mcp;
                }
                "assistant" => assistant_message_tokens += content_tokens,
                "tool" => tool_result_tokens += content_tokens,
                _ => {
                    if is_summary_message(msg.get("content")) {
                        summarized_conversation_tokens += content_tokens;
                    } else if is_subagent_message(msg.get("content")) {
                        subagent_tokens += content_tokens;
                    } else {
                        user_message_tokens += content_tokens;
                    }
                }
            }
        }
    }

    let tool_definition_tokens = body
        .get("tools")
        .and_then(|t| t.as_array())
        .map_or(0, |arr| json_chars(arr) / 4);

    let tool_definition_count = body
        .get("tools")
        .and_then(|t| t.as_array())
        .map_or(0, Vec::len);

    let conversation_tokens = user_message_tokens + assistant_message_tokens;

    let total_input_tokens = system_prompt_tokens
        + rules_tokens
        + skills_tokens
        + mcp_config_tokens
        + user_message_tokens
        + assistant_message_tokens
        + tool_definition_tokens
        + tool_result_tokens
        + subagent_tokens
        + summarized_conversation_tokens;

    RequestBreakdown {
        provider: Provider::OpenAi,
        model,
        system_prompt_tokens,
        user_message_tokens,
        assistant_message_tokens,
        tool_definition_tokens,
        tool_definition_count,
        tool_result_tokens,
        image_count,
        total_input_tokens,
        message_count,
        rules_tokens,
        skills_tokens,
        mcp_config_tokens,
        subagent_tokens,
        summarized_conversation_tokens,
        conversation_tokens,
    }
}

fn analyze_gemini(body: &Value) -> RequestBreakdown {
    let model = "gemini".to_string();

    let system_prompt_tokens = body
        .get("systemInstruction")
        .and_then(|si| si.get("parts"))
        .and_then(|p| p.as_array())
        .map_or(0, |parts| {
            parts
                .iter()
                .map(|p| p.get("text").and_then(|t| t.as_str()).map_or(0, str::len))
                .sum::<usize>()
                / 4
        });

    let mut user_message_tokens = 0;
    let mut assistant_message_tokens = 0;
    let mut tool_result_tokens = 0;
    let mut message_count = 0;

    if let Some(contents) = body.get("contents").and_then(|c| c.as_array()) {
        message_count = contents.len();
        for content in contents {
            let role = content
                .get("role")
                .and_then(|r| r.as_str())
                .unwrap_or("user");
            let parts_tokens = content
                .get("parts")
                .and_then(|p| p.as_array())
                .map_or(0, |parts| {
                    parts
                        .iter()
                        .map(|p| {
                            if p.get("functionResponse").is_some() {
                                json_chars(std::slice::from_ref(p)) / 4
                            } else {
                                p.get("text")
                                    .and_then(|t| t.as_str())
                                    .map_or(0, |s| chars_to_tokens(s.len()))
                            }
                        })
                        .sum::<usize>()
                });

            let has_fn_response = content
                .get("parts")
                .and_then(|p| p.as_array())
                .is_some_and(|parts| parts.iter().any(|p| p.get("functionResponse").is_some()));

            if has_fn_response {
                tool_result_tokens += parts_tokens;
            } else {
                match role {
                    "model" => assistant_message_tokens += parts_tokens,
                    _ => user_message_tokens += parts_tokens,
                }
            }
        }
    }

    let tool_definition_tokens = body
        .get("tools")
        .and_then(|t| t.as_array())
        .map_or(0, |arr| json_chars(arr) / 4);

    let tool_definition_count = body
        .get("tools")
        .and_then(|t| t.as_array())
        .map_or(0, |arr| {
            arr.iter()
                .filter_map(|t| t.get("functionDeclarations").and_then(|f| f.as_array()))
                .map(Vec::len)
                .sum()
        });

    let total_input_tokens = system_prompt_tokens
        + user_message_tokens
        + assistant_message_tokens
        + tool_definition_tokens
        + tool_result_tokens;

    let conversation_tokens = user_message_tokens + assistant_message_tokens;

    RequestBreakdown {
        provider: Provider::Gemini,
        model,
        system_prompt_tokens,
        user_message_tokens,
        assistant_message_tokens,
        tool_definition_tokens,
        tool_definition_count,
        tool_result_tokens,
        image_count: 0,
        total_input_tokens,
        message_count,
        rules_tokens: 0,
        skills_tokens: 0,
        mcp_config_tokens: 0,
        subagent_tokens: 0,
        summarized_conversation_tokens: 0,
        conversation_tokens,
    }
}

fn chars_to_tokens(chars: usize) -> usize {
    chars / 4
}

fn json_chars(arr: &[Value]) -> usize {
    arr.iter().map(|v| v.to_string().len()).sum()
}

fn estimate_content_tokens(content: Option<&Value>) -> usize {
    match content {
        Some(Value::String(s)) => chars_to_tokens(s.len()),
        Some(Value::Array(arr)) => arr
            .iter()
            .map(|block| {
                if let Some(text) = block.get("text").and_then(|t| t.as_str()) {
                    chars_to_tokens(text.len())
                } else {
                    block.to_string().len() / 4
                }
            })
            .sum(),
        Some(v) => v.to_string().len() / 4,
        None => 0,
    }
}

fn count_images(content: Option<&Value>) -> usize {
    match content {
        Some(Value::Array(arr)) => arr
            .iter()
            .filter(|block| {
                block.get("type").and_then(|t| t.as_str()) == Some("image")
                    || block.get("type").and_then(|t| t.as_str()) == Some("image_url")
            })
            .count(),
        _ => 0,
    }
}

struct SystemPromptParts {
    base: usize,
    rules: usize,
    skills: usize,
    mcp: usize,
}

fn classify_system_prompt(text: &str) -> SystemPromptParts {
    let mut rules = 0usize;
    let mut skills = 0usize;
    let mut mcp = 0usize;
    let mut base = 0usize;

    let rule_markers = [
        "<always_applied_workspace_rule",
        "<user_rule",
        ".cursorrules",
        "AGENTS.md",
        ".mdc",
        "workspace_rule",
        "cursor_rules",
        "CLAUDE.md",
        "<rules>",
    ];
    let skill_markers = [
        "<agent_skill",
        "<available_skills",
        "SKILL.md",
        "skills-cursor",
        "agent_skills",
    ];
    let mcp_markers = [
        "<mcp_file_system",
        "mcp_server",
        "MCP server",
        "CallMcpTool",
        "FetchMcpResource",
        "<mcp_file_system_server",
    ];

    for line in text.lines() {
        let tok = chars_to_tokens(line.len() + 1);
        let l = line.trim();

        if rule_markers.iter().any(|m| l.contains(m)) {
            rules += tok;
        } else if skill_markers.iter().any(|m| l.contains(m)) {
            skills += tok;
        } else if mcp_markers.iter().any(|m| l.contains(m)) {
            mcp += tok;
        } else {
            base += tok;
        }
    }

    SystemPromptParts {
        base,
        rules,
        skills,
        mcp,
    }
}

fn is_summary_message(content: Option<&Value>) -> bool {
    let text = extract_text_content(content);
    text.contains("[Previous conversation summary]")
        || text.contains("conversation summary")
        || text.contains("Here is a summary of the conversation")
        || text.contains("summarized conversation")
}

fn is_subagent_message(content: Option<&Value>) -> bool {
    let text = extract_text_content(content);
    text.contains("subagent")
        || text.contains("background agent")
        || text.contains("<task>")
        || text.contains("system_notification")
}

fn extract_text_content(content: Option<&Value>) -> String {
    match content {
        Some(Value::String(s)) => s.clone(),
        Some(Value::Array(arr)) => arr
            .iter()
            .filter_map(|b| b.get("text").and_then(|t| t.as_str()))
            .collect::<Vec<_>>()
            .join(" "),
        _ => String::new(),
    }
}

fn has_tool_results(content: Option<&Value>) -> bool {
    match content {
        Some(Value::Array(arr)) => arr
            .iter()
            .any(|block| block.get("type").and_then(|t| t.as_str()) == Some("tool_result")),
        _ => false,
    }
}

pub struct IntrospectState {
    pub last_breakdown: Mutex<Option<RequestBreakdown>>,
    pub total_system_prompt_tokens: AtomicU64,
    pub total_requests: AtomicU64,
    last_persist_epoch: AtomicU64,
}

impl Default for IntrospectState {
    fn default() -> Self {
        Self {
            last_breakdown: Mutex::new(None),
            total_system_prompt_tokens: AtomicU64::new(0),
            total_requests: AtomicU64::new(0),
            last_persist_epoch: AtomicU64::new(0),
        }
    }
}

impl IntrospectState {
    pub fn record(&self, breakdown: RequestBreakdown) {
        self.total_system_prompt_tokens.fetch_add(
            (breakdown.system_prompt_tokens
                + breakdown.rules_tokens
                + breakdown.skills_tokens
                + breakdown.mcp_config_tokens) as u64,
            Ordering::Relaxed,
        );
        self.total_requests.fetch_add(1, Ordering::Relaxed);
        if let Ok(mut last) = self.last_breakdown.lock() {
            *last = Some(breakdown);
        }
        self.maybe_persist();
    }

    fn maybe_persist(&self) {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        let prev = self.last_persist_epoch.load(Ordering::Relaxed);
        if now <= prev {
            return;
        }
        if self
            .last_persist_epoch
            .compare_exchange(prev, now, Ordering::AcqRel, Ordering::Relaxed)
            .is_err()
        {
            return;
        }
        self.persist(now);
    }

    fn persist(&self, ts: u64) {
        let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() else {
            return;
        };
        let breakdown_val = self
            .last_breakdown
            .lock()
            .ok()
            .and_then(|guard| guard.as_ref().map(|b| serde_json::to_value(b).ok()))
            .flatten();
        let payload = serde_json::json!({
            "ts": ts,
            "proxy_active": true,
            "last_breakdown": breakdown_val,
            "cumulative": {
                "total_requests": self.total_requests.load(Ordering::Relaxed),
                "total_system_prompt_tokens": self.total_system_prompt_tokens.load(Ordering::Relaxed),
            }
        });

        let target = data_dir.join("proxy-introspect.json");
        let tmp = data_dir.join("proxy-introspect.json.tmp");
        if let Ok(json) = serde_json::to_string_pretty(&payload) {
            if std::fs::write(&tmp, &json).is_ok() {
                let _ = std::fs::rename(&tmp, &target);
            }
        }
    }
}

/// Load persisted proxy introspection data from disk.
/// Returns `None` if the file doesn't exist or is stale (> `max_age_secs`).
pub fn load_persisted(max_age_secs: u64) -> Option<serde_json::Value> {
    let data_dir = crate::core::data_dir::lean_ctx_data_dir().ok()?;
    let path = data_dir.join("proxy-introspect.json");
    let content = std::fs::read_to_string(&path).ok()?;
    let val: serde_json::Value = serde_json::from_str(&content).ok()?;

    let ts = val
        .get("ts")
        .and_then(serde_json::Value::as_u64)
        .unwrap_or(0);
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    if now.saturating_sub(ts) > max_age_secs {
        return None;
    }
    Some(val)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn anthropic_basic() {
        let body = serde_json::json!({
            "model": "claude-sonnet-4-20250514",
            "system": "You are a helpful assistant.",
            "messages": [
                {"role": "user", "content": "Hello"},
                {"role": "assistant", "content": "Hi there!"}
            ],
            "tools": [{"name": "read", "description": "Read a file", "input_schema": {}}]
        });
        let b = analyze_request(&body, Provider::Anthropic);
        assert_eq!(b.provider, Provider::Anthropic);
        assert!(b.system_prompt_tokens > 0);
        assert_eq!(b.message_count, 2);
        assert!(b.user_message_tokens > 0);
        assert!(b.assistant_message_tokens > 0);
        assert_eq!(b.tool_definition_count, 1);
        assert!(b.tool_definition_tokens > 0);
    }

    #[test]
    fn openai_system_message() {
        let body = serde_json::json!({
            "model": "gpt-4o",
            "messages": [
                {"role": "system", "content": "System prompt here"},
                {"role": "user", "content": "Hello"},
                {"role": "tool", "content": "tool result data", "tool_call_id": "x"}
            ]
        });
        let b = analyze_request(&body, Provider::OpenAi);
        assert!(b.system_prompt_tokens > 0);
        assert!(b.user_message_tokens > 0);
        assert!(b.tool_result_tokens > 0);
        assert_eq!(b.message_count, 3);
    }

    #[test]
    fn gemini_system_instruction() {
        let body = serde_json::json!({
            "systemInstruction": {
                "parts": [{"text": "Be concise and helpful to the user at all times."}]
            },
            "contents": [
                {"role": "user", "parts": [{"text": "What is the meaning of life and everything?"}]},
                {"role": "model", "parts": [{"text": "The answer is 42 according to Douglas Adams."}]}
            ]
        });
        let b = analyze_request(&body, Provider::Gemini);
        assert!(b.system_prompt_tokens > 0);
        assert!(b.user_message_tokens > 0);
        assert!(b.assistant_message_tokens > 0);
        assert_eq!(b.message_count, 2);
    }
}