1const CHARS_PER_TOKEN: usize = 4;
11
12pub fn estimate_tokens(s: &str) -> usize {
14 s.len().div_ceil(CHARS_PER_TOKEN)
15}
16
17pub fn estimate_json_mode_tokens(query: &str, records: &[&str]) -> usize {
21 let envelope = 120;
24 let per_record = 50;
25 let mut total = envelope + estimate_tokens(query);
26 for r in records {
27 total += per_record / CHARS_PER_TOKEN;
28 total += estimate_tokens(r);
29 }
30 total
31}
32
33pub fn estimate_code_mode_tokens(query: &str, records: &[&str], host_calls: usize) -> usize {
38 let mut total = estimate_tokens(query) + host_calls * 4;
39 for r in records {
40 total += estimate_tokens(r);
41 }
42 total
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn estimate_is_monotonic_in_length() {
51 let a = estimate_tokens("hi");
52 let b = estimate_tokens("hi there friend");
53 assert!(b > a);
54 }
55
56 #[test]
57 fn json_mode_costs_more_than_code_mode() {
58 let query = "find me notes about the patient";
59 let records: Vec<String> = (0..5)
60 .map(|i| format!("Patient note {i}: persistent fatigue, hemoglobin low."))
61 .collect();
62 let refs: Vec<&str> = records.iter().map(|s| s.as_str()).collect();
63 let json = estimate_json_mode_tokens(query, &refs);
64 let code = estimate_code_mode_tokens(query, &refs, 1);
65 assert!(
66 json > code,
67 "expected json > code, got json={json} code={code}"
68 );
69 }
70
71 #[test]
72 fn long_conversation_savings_exceed_50_percent() {
73 let query = "what was discussed last time";
76 let records: Vec<String> = (0..5)
77 .map(|i| {
78 format!(
79 "Memory {i}: the patient discussed {} on a prior visit, lab values were within range.",
80 "treatment"
81 )
82 })
83 .collect();
84 let refs: Vec<&str> = records.iter().map(|s| s.as_str()).collect();
85 let json: usize = (0..200)
86 .map(|_| estimate_json_mode_tokens(query, &refs))
87 .sum();
88 let code: usize = (0..200)
89 .map(|_| estimate_code_mode_tokens(query, &refs, 1))
90 .sum();
91 assert!(
97 code * 100 / json <= 80,
98 "expected code-mode <= 80% of json-mode tokens, got json={json} code={code} \
99 ratio={}%",
100 code * 100 / json
101 );
102 }
103}