aither-core 0.25.0

Core trait abstractions for aither
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
//! Exact in-process token counting for models whose vocabulary is published.
//!
//! A [`Vocabulary`] wraps one embedded BPE vocabulary from the `tiktoken`
//! crate — each family lives behind its own feature (`vocab-o200k`,
//! `vocab-cl100k`, `vocab-deepseek`, `vocab-llama`, `vocab-mistral`,
//! `vocab-qwen`), so a client carries only the vocabularies its models can
//! use. [`Vocabulary::for_model`] maps a model id to the published
//! vocabulary it counts with, or `None` for a model with no local counter.

extern crate std;

use alloc::string::ToString;
use std::io::Cursor;

use base64::Engine as _;
use num_traits::ToPrimitive as _;

use crate::llm::LLMRequest;

/// Per-message chat framing, per `OpenAI`'s published recipe: the start,
/// role and end markers of each message.
const TOKENS_PER_MESSAGE: u32 = 3;

/// The primer the model's reply is opened with.
const TOKENS_PER_REPLY: u32 = 3;

/// Image tokens in low-detail mode: a flat cost regardless of size.
const IMAGE_LOW_DETAIL_TOKENS: u32 = 85;

/// Image tokens per 512×512 tile in high-detail mode.
const IMAGE_TILE_TOKENS: u32 = 170;

/// Base cost every high-detail image pays on top of its tiles.
const IMAGE_BASE_TOKENS: u32 = 85;

/// The encoding behind a [`Vocabulary`], for callers that hold the engine
/// themselves.
#[must_use]
pub fn encoding_for_model(model_id: &str) -> Option<&'static str> {
    let id = model_id
        .rsplit('/')
        .next()
        .unwrap_or(model_id)
        .to_ascii_lowercase();
    // OpenAI dates its snapshots "-YYYY-MM-DD"; drop that tail.
    let id = id
        .get(..id.len().saturating_sub(11))
        .filter(|_| {
            id.as_bytes().last().is_some_and(u8::is_ascii_digit)
                && id
                    .as_bytes()
                    .get(id.len().saturating_sub(11))
                    .is_some_and(|b| *b == b'-')
        })
        .unwrap_or(&id);
    if id.starts_with("deepseek") {
        #[cfg(feature = "vocab-deepseek")]
        {
            return Some("deepseek_v3");
        }
    } else if id.starts_with("qwen") {
        #[cfg(feature = "vocab-qwen")]
        {
            return Some("qwen2");
        }
    } else if id.starts_with("llama-3") || id.starts_with("llama3") {
        #[cfg(feature = "vocab-llama")]
        {
            return Some("llama3");
        }
    } else if id.starts_with("mistral")
        || id.starts_with("codestral")
        || id.starts_with("ministral")
        || id.starts_with("devstral")
        || id.starts_with("magistral")
        || id.starts_with("pixtral")
    {
        #[cfg(feature = "vocab-mistral")]
        {
            return Some("mistral_v3");
        }
    } else if id.starts_with("gpt-4o")
        || id.starts_with("gpt-4.1")
        || id.starts_with("gpt-5")
        || id.starts_with('o') && (id.as_bytes().get(1).is_some_and(u8::is_ascii_digit))
        || id.starts_with("gpt-oss")
        || id.starts_with("codex")
    {
        #[cfg(feature = "vocab-o200k")]
        {
            return Some("o200k_base");
        }
    } else if id.starts_with("gpt-4") || id.starts_with("gpt-3.5") {
        #[cfg(feature = "vocab-cl100k")]
        {
            return Some("cl100k_base");
        }
    }
    let _ = id;
    None
}

/// An embedded BPE vocabulary a model counts with, loaded once and held by
/// the client it serves. The engine is a cached `'static` handle, so a
/// `Vocabulary` is a cheap `Copy` handle into it.
#[derive(Clone, Copy)]
pub struct Vocabulary {
    encoding: &'static str,
    bpe: &'static tiktoken::CoreBpe,
}

impl core::fmt::Debug for Vocabulary {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Vocabulary")
            .field("encoding", &self.encoding)
            .finish_non_exhaustive()
    }
}

impl Vocabulary {
    /// The vocabulary `model_id` counts with, or `None` when no published
    /// vocabulary covers it — Claude, Gemini, and ids the table does not
    /// know count through an endpoint or not at all.
    ///
    /// Provider prefixes (`openai/gpt-4o`) and dated snapshot suffixes
    /// (`gpt-4o-2024-08-06`) are stripped before the match; unknown
    /// families fall through to `None` rather than guessing.
    #[must_use]
    pub fn for_model(model_id: &str) -> Option<Self> {
        let encoding = encoding_for_model(model_id)?;
        let bpe = tiktoken::get_encoding(encoding)?;
        Some(Self { encoding, bpe })
    }

    /// The name of the embedded encoding this vocabulary wraps.
    #[must_use]
    pub const fn encoding(&self) -> &'static str {
        self.encoding
    }

    /// The tokens `text` takes in this vocabulary.
    #[must_use]
    pub fn count_text(&self, text: &str) -> u32 {
        u32::try_from(self.bpe.count(text)).unwrap_or(u32::MAX)
    }

    /// The prompt tokens `request` costs: per-message chat framing and the
    /// reply primer from the published recipe, tool-call names and
    /// arguments, tool definitions at their OpenAI-schema size, and images
    /// at the published tile cost.
    ///
    /// Tolerance: the provider's own serialization overhead — role names,
    /// separators and envelope fields the recipe does not publish — so a
    /// count can run a few tokens per message under or over the reported
    /// `prompt_tokens`. Attachments that are not decodable images are not
    /// counted; the budget's usage correction absorbs the rest.
    #[must_use]
    pub fn count_request(&self, request: &LLMRequest) -> u32 {
        let mut total = TOKENS_PER_REPLY;
        for message in request.messages() {
            total += TOKENS_PER_MESSAGE;
            total += self.count_text(message.content());
            for call in message.tool_calls() {
                total += self.count_text(&call.name) + self.count_text(&call.arguments.to_string());
            }
            for attachment in message.attachments() {
                if let Some(tokens) = attachment_tokens(attachment) {
                    total += tokens;
                }
            }
        }
        for definition in request.tool_definitions() {
            total += self.count_text(definition.name())
                + self.count_text(definition.description())
                + self.count_text(&definition.arguments_openai_schema().to_string());
        }
        total
    }
}

/// The tokens one attachment costs, when it is an image whose dimensions
/// can be read: the published high-detail tile formula for the GPT-4o/4.1
/// family. Anything else returns `None` and is not counted.
fn attachment_tokens(attachment: &crate::llm::Attachment) -> Option<u32> {
    let (width, height) = attachment_dimensions(attachment)?;
    Some(image_tokens(width, height))
}

/// Reads image dimensions out of a `data:` URL attachment. Remote or
/// unparseable attachments return `None`.
fn attachment_dimensions(attachment: &crate::llm::Attachment) -> Option<(u32, u32)> {
    if !attachment
        .media_type()
        .type_()
        .as_str()
        .starts_with("image")
    {
        return None;
    }
    let url = attachment.url();
    if url.scheme() != "data" {
        return None;
    }
    let path = url.path();
    let (_, data) = path.split_once(',')?;
    let bytes = base64::engine::general_purpose::STANDARD
        .decode(data)
        .ok()?;
    image::ImageReader::new(Cursor::new(bytes))
        .with_guessed_format()
        .ok()?
        .into_dimensions()
        .ok()
}

/// The published GPT-4o/4.1 high-detail formula: scale to fit 2048², then
/// scale the shortest side to 768px, split into 512×512 tiles, and charge
/// base + tile tokens.
fn image_tokens(width: u32, height: u32) -> u32 {
    if width == 0 || height == 0 {
        return IMAGE_LOW_DETAIL_TOKENS;
    }
    let (mut w, mut h) = (f64::from(width), f64::from(height));
    if w > 2048.0 || h > 2048.0 {
        let scale = 2048.0 / w.max(h);
        w *= scale;
        h *= scale;
    }
    let shortest = w.min(h);
    if shortest > 768.0 {
        let scale = 768.0 / shortest;
        w *= scale;
        h *= scale;
    }
    let tiles = (w / 512.0).ceil() * (h / 512.0).ceil();
    IMAGE_BASE_TOKENS + IMAGE_TILE_TOKENS * tiles.to_u32().unwrap_or(u32::MAX)
}

/// `Vocabulary` is shared across threads by the clients that hold it.
const _: () = {
    const fn check<T: Send + Sync>() {}
    check::<Vocabulary>();
};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::llm::Message;
    use alloc::format;
    use alloc::vec::Vec;
    use url::Url;

    #[test]
    fn model_ids_map_to_their_published_vocabularies() {
        assert_eq!(encoding_for_model("gpt-4o"), Some("o200k_base"));
        assert_eq!(encoding_for_model("gpt-4o-2024-08-06"), Some("o200k_base"));
        assert_eq!(encoding_for_model("openai/gpt-5.1"), Some("o200k_base"));
        assert_eq!(encoding_for_model("o4-mini"), Some("o200k_base"));
        assert_eq!(encoding_for_model("gpt-4-turbo"), Some("cl100k_base"));
        assert_eq!(encoding_for_model("deepseek-chat"), Some("deepseek_v3"));
        assert_eq!(
            encoding_for_model("deepseek/DeepSeek-R1"),
            Some("deepseek_v3")
        );
        assert_eq!(encoding_for_model("qwen3-coder"), Some("qwen2"));
        assert_eq!(encoding_for_model("llama-3.3-70b"), Some("llama3"));
        assert_eq!(
            encoding_for_model("mistral-large-latest"),
            Some("mistral_v3")
        );
        assert_eq!(encoding_for_model("codestral-latest"), Some("mistral_v3"));
        assert_eq!(encoding_for_model("claude-opus-4-5"), None);
        assert_eq!(encoding_for_model("gemini-3-pro"), None);
        assert_eq!(encoding_for_model("grok-4"), None);
    }

    #[test]
    fn counting_text_uses_the_embedded_vocabulary() {
        let vocab = Vocabulary::for_model("gpt-4o").expect("o200k is published");
        // tiktoken's own reference count for cl100k/o200k family.
        assert_eq!(vocab.count_text("hello world"), 2);
        let cjk = vocab.count_text("中文、日本語、한국어 텍스트를 섞어서 토큰을 세어봅니다");
        assert!(cjk > 0);
        assert_eq!(
            cjk,
            vocab.count_text("中文、日本語、한국어 텍스트를 섞어서 토큰을 세어봅니다")
        );
        assert!(vocab.count_text("hello world") < cjk);
    }

    #[test]
    fn every_vocabulary_family_counts() {
        for model in [
            "gpt-4o",
            "deepseek-r1",
            "qwen2.5",
            "llama-3.1-70b",
            "mistral-large",
        ] {
            let vocab = Vocabulary::for_model(model).expect("published family");
            let text = "Translate this sentence into French";
            let count = vocab.count_text(text);
            assert!(count > 0, "{model}");
            assert_eq!(count, vocab.count_text(text), "{model}");
        }
    }

    #[test]
    fn counting_a_request_charges_framing_tools_and_images() {
        let vocab = Vocabulary::for_model("deepseek-r1").expect("published");
        let request = LLMRequest::new([
            Message::user("describe this screenshot"),
            Message::assistant("it is a chart"),
        ]);
        let bare = vocab.count_request(&LLMRequest::new([Message::user(
            "describe this screenshot",
        )]));
        // Two messages: reply primer + per-message framing + content tokens.
        assert!(bare > 3 + 3, "framing is charged on top of content");
        let two = vocab.count_request(&request);
        assert_eq!(
            two - bare,
            TOKENS_PER_MESSAGE + vocab.count_text("it is a chart")
        );
    }

    /// A complete PNG: signature, IHDR, one IDAT holding a stored deflate
    /// block, and IEND — enough structure for `into_dimensions`.
    fn png_header(width: u32, height: u32) -> Vec<u8> {
        fn crc32(bytes: &[u8]) -> u32 {
            let mut crc = !0_u32;
            for byte in bytes {
                crc ^= u32::from(*byte);
                for _ in 0..8 {
                    crc = if crc & 1 == 1 {
                        crc >> 1 ^ 0xEDB8_8320
                    } else {
                        crc >> 1
                    };
                }
            }
            !crc
        }
        fn adler32(bytes: &[u8]) -> u32 {
            let (mut a, mut b) = (1_u32, 0_u32);
            for byte in bytes {
                a = (a + u32::from(*byte)) % 65521;
                b = (b + a) % 65521;
            }
            b << 16 | a
        }
        fn chunk(kind: [u8; 4], data: &[u8]) -> Vec<u8> {
            let mut chunk = u32::try_from(data.len())
                .expect("test chunks are small")
                .to_be_bytes()
                .to_vec();
            chunk.extend_from_slice(&kind);
            chunk.extend_from_slice(data);
            let mut checked = kind.to_vec();
            checked.extend_from_slice(data);
            chunk.extend_from_slice(&crc32(&checked).to_be_bytes());
            chunk
        }
        let mut ihdr = Vec::new();
        ihdr.extend_from_slice(&width.to_be_bytes());
        ihdr.extend_from_slice(&height.to_be_bytes());
        ihdr.extend_from_slice(&[8, 6, 0, 0, 0]);
        // zlib: header + stored deflate block wrapping 5 bytes + adler32.
        let scanline = [0, 0, 0, 0, 0];
        let mut idat = Vec::from(&[0x78, 0x01, 0x01][..]);
        let len16 = u16::try_from(scanline.len()).expect("tiny scanline");
        idat.extend_from_slice(&len16.to_le_bytes());
        idat.extend_from_slice(&(!len16).to_le_bytes());
        idat.extend_from_slice(&scanline);
        idat.extend_from_slice(&adler32(&scanline).to_be_bytes());
        let mut bytes = Vec::from(&b"\x89PNG\r\n\x1a\n"[..]);
        bytes.extend_from_slice(&chunk(*b"IHDR", &ihdr));
        bytes.extend_from_slice(&chunk(*b"IDAT", &idat));
        bytes.extend_from_slice(&chunk(*b"IEND", &[]));
        bytes
    }

    #[test]
    fn image_attachments_cost_their_tiles() {
        let data = format!(
            "data:image/png;base64,{}",
            base64::engine::general_purpose::STANDARD.encode(png_header(1024, 768))
        );
        let attachment = crate::llm::Attachment::new(
            Url::parse(&data).expect("data url parses"),
            "image/png".parse().expect("mime"),
        );
        // 1024×768: shortest side already 768, so 2×2 tiles → 85 + 4×170.
        assert_eq!(attachment_tokens(&attachment), Some(765));
        let remote = crate::llm::Attachment::new(
            Url::parse("https://example.com/shot.png").expect("url"),
            "image/png".parse().expect("mime"),
        );
        assert_eq!(attachment_tokens(&remote), None);
        let pdf = crate::llm::Attachment::new(
            Url::parse(&data).expect("data url parses"),
            "application/pdf".parse().expect("mime"),
        );
        assert_eq!(attachment_tokens(&pdf), None);
    }
}