llmshim 0.13.0

Blazing fast LLM API translation layer in pure Rust
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
use llmshim::provider::Provider;
use llmshim::providers::openrouter::OpenRouter;
use serde_json::{json, Value};

fn provider() -> OpenRouter {
    OpenRouter::new("test-key-abc".into())
}

// ============================================================
// transform_request
// ============================================================

#[test]
fn request_url_and_bearer_auth() {
    let p = provider();
    let req = json!({
        "model": "anthropic/claude-sonnet-4.5",
        "messages": [{"role": "user", "content": "hi"}],
    });
    let result = p
        .transform_request("anthropic/claude-sonnet-4.5", &req)
        .unwrap();
    assert_eq!(result.url, "https://openrouter.ai/api/v1/chat/completions");
    let auth = result.headers.iter().find(|(k, _)| k == "Authorization");
    assert_eq!(auth.unwrap().1, "Bearer test-key-abc");
}

#[test]
fn request_preserves_slug_and_messages() {
    // The vendor/model slug (with its internal slash) is passed straight through.
    let p = provider();
    let req = json!({
        "model": "meta-llama/llama-3.1-70b-instruct",
        "messages": [{"role": "user", "content": "hi"}],
    });
    let result = p
        .transform_request("meta-llama/llama-3.1-70b-instruct", &req)
        .unwrap();
    assert_eq!(result.body["model"], "meta-llama/llama-3.1-70b-instruct");
    assert_eq!(result.body["messages"][0]["content"], "hi");
}

#[test]
fn request_preserves_a_variant_suffix_on_the_wire() {
    // The catalog lookup normalizes a suffix like `:nitro` off for
    // metadata (family/window/price/capabilities), but the suffix is a
    // routing hint OpenRouter reads off the wire model string itself, so the
    // built request body must carry it exactly as given.
    let p = provider();
    let model = "deepseek/deepseek-v4.1-flash:nitro";
    let req = json!({
        "model": model,
        "messages": [{"role": "user", "content": "hi"}],
    });
    let result = p.transform_request(model, &req).unwrap();
    assert_eq!(result.body["model"], model);
}

#[test]
fn request_forwards_tools_unchanged() {
    // OpenRouter is Chat Completions-native, so nested tools need no translation.
    let p = provider();
    let tools = json!([{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather",
            "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}
        }
    }]);
    let req = json!({
        "model": "openai/gpt-5.1",
        "messages": [{"role": "user", "content": "hi"}],
        "tools": tools,
        "tool_choice": "auto",
        "response_format": {"type": "json_object"},
    });
    let result = p.transform_request("openai/gpt-5.1", &req).unwrap();
    assert_eq!(result.body["tools"], tools);
    assert_eq!(result.body["tool_choice"], "auto");
    assert_eq!(result.body["response_format"]["type"], "json_object");
}

#[test]
fn request_maps_reasoning_effort_1to1() {
    let p = provider();
    for effort in ["low", "medium", "high", "xhigh", "max", "none"] {
        let req = json!({
            "model": "x/y",
            "messages": [{"role": "user", "content": "hi"}],
            "reasoning_effort": effort,
        });
        let result = p.transform_request("x/y", &req).unwrap();
        assert_eq!(
            result.body["reasoning"]["effort"], effort,
            "effort {effort} should map 1:1 (OpenRouter vocab is a superset)"
        );
    }
}

#[test]
fn request_reasoning_mode_pro_bumps_one_tier() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [{"role": "user", "content": "hi"}],
        "reasoning_effort": "high",
        "reasoning_mode": "pro",
    });
    let result = p.transform_request("x/y", &req).unwrap();
    assert_eq!(result.body["reasoning"]["effort"], "xhigh");
}

#[test]
fn request_native_reasoning_wins_over_effort() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [{"role": "user", "content": "hi"}],
        "reasoning_effort": "high",
        "x-openrouter": {"reasoning": {"max_tokens": 2000}},
    });
    let result = p.transform_request("x/y", &req).unwrap();
    assert_eq!(result.body["reasoning"]["max_tokens"], 2000);
    // The unified effort mapping is bypassed when native reasoning is supplied.
    assert!(result.body["reasoning"].get("effort").is_none());
}

#[test]
fn request_disables_middle_out_by_default() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [{"role": "user", "content": "hi"}],
    });
    let result = p.transform_request("x/y", &req).unwrap();
    assert_eq!(result.body["transforms"], json!([]));
}

#[test]
fn request_transforms_opt_in_preserved() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [{"role": "user", "content": "hi"}],
        "x-openrouter": {"transforms": ["middle-out"]},
    });
    let result = p.transform_request("x/y", &req).unwrap();
    assert_eq!(result.body["transforms"], json!(["middle-out"]));
}

#[test]
fn request_x_openrouter_body_passthrough() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [{"role": "user", "content": "hi"}],
        "x-openrouter": {
            "provider": {"order": ["anthropic", "openai"], "allow_fallbacks": false},
            "models": ["anthropic/claude-sonnet-4.5", "openai/gpt-5.1"],
            "route": "fallback"
        },
    });
    let result = p.transform_request("x/y", &req).unwrap();
    assert_eq!(result.body["provider"]["order"][0], "anthropic");
    assert_eq!(result.body["provider"]["allow_fallbacks"], false);
    assert_eq!(result.body["models"][1], "openai/gpt-5.1");
    assert_eq!(result.body["route"], "fallback");
}

#[test]
fn request_x_openrouter_attribution_headers_not_in_body() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [{"role": "user", "content": "hi"}],
        "x-openrouter": {"http_referer": "https://example.com", "x_title": "MyApp"},
    });
    let result = p.transform_request("x/y", &req).unwrap();
    let referer = result.headers.iter().find(|(k, _)| k == "HTTP-Referer");
    let title = result.headers.iter().find(|(k, _)| k == "X-Title");
    assert_eq!(referer.unwrap().1, "https://example.com");
    assert_eq!(title.unwrap().1, "MyApp");
    // Attribution controls must not leak into the request body.
    assert!(result.body.get("http_referer").is_none());
    assert!(result.body.get("x_title").is_none());
}

#[test]
fn request_sanitizes_foreign_reasoning_fields() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [{
            "role": "assistant",
            "content": "hi",
            "reasoning_content": "x",
            "reasoning_signature": "anthropic-sig-should-not-leak",
            "redacted_reasoning_content": "redacted-should-not-leak"
        }]
    });
    let result = p.transform_request("x/y", &req).unwrap();
    let body = serde_json::to_string(&result.body).unwrap();
    assert!(!body.contains("anthropic-sig-should-not-leak"));
    assert!(!body.contains("redacted-should-not-leak"));
    assert!(!body.contains("reasoning_signature"));
}

#[test]
fn request_preserves_vision_and_tool_messages() {
    let p = provider();
    let req = json!({
        "model": "x/y",
        "messages": [
            {"role": "user", "content": [
                {"type": "text", "text": "what is this?"},
                {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
            ]},
            {"role": "assistant", "content": "", "tool_calls": [{
                "id": "call_1", "type": "function",
                "function": {"name": "f", "arguments": "{}"}
            }]},
            {"role": "tool", "tool_call_id": "call_1", "content": "result"}
        ]
    });
    let result = p.transform_request("x/y", &req).unwrap();
    let msgs = result.body["messages"].as_array().unwrap();
    // Image block preserved in OpenAI form.
    assert_eq!(msgs[0]["content"][1]["type"], "image_url");
    // tool_calls stay in Chat Completions shape (no function_call splitting).
    assert_eq!(msgs[1]["tool_calls"][0]["id"], "call_1");
    // role:"tool" stays as-is.
    assert_eq!(msgs[2]["role"], "tool");
    assert_eq!(msgs[2]["tool_call_id"], "call_1");
}

// ============================================================
// transform_response
// ============================================================

#[test]
fn response_normalizes_reasoning_to_reasoning_content() {
    let p = provider();
    let resp = json!({
        "id": "gen_1",
        "model": "anthropic/claude-sonnet-4.5",
        "object": "chat.completion",
        "choices": [{
            "index": 0,
            "message": {"role": "assistant", "content": "42", "reasoning": "let me think..."},
            "finish_reason": "stop"
        }],
        "usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7}
    });
    let result = p
        .transform_response("anthropic/claude-sonnet-4.5", resp)
        .unwrap();
    let msg = &result["choices"][0]["message"];
    assert_eq!(msg["reasoning"][0]["text"], "let me think...");
    assert_eq!(msg["content"], "42");
    assert_eq!(result["usage"]["total_tokens"], 7);
}

#[test]
fn response_passes_tool_calls_through() {
    let p = provider();
    let resp = json!({
        "choices": [{
            "index": 0,
            "message": {"role": "assistant", "content": null, "tool_calls": [{
                "id": "call_9", "type": "function",
                "function": {"name": "search", "arguments": "{\"q\":\"x\"}"}
            }]},
            "finish_reason": "tool_calls"
        }]
    });
    let result = p.transform_response("x/y", resp).unwrap();
    let tc = &result["choices"][0]["message"]["tool_calls"][0];
    assert_eq!(tc["wire_ids"][0]["id"], "call_9");
    assert_eq!(tc["function"]["name"], "search");
    assert_eq!(result["choices"][0]["finish_reason"], "tool_calls");
}

#[test]
fn response_error_object_becomes_error() {
    let p = provider();
    let resp = json!({"error": {"code": 402, "message": "Insufficient credits"}});
    let result = p.transform_response("x/y", resp);
    assert!(result.is_err());
}

// ============================================================
// transform_stream_chunk
// ============================================================

#[test]
fn stream_normalizes_reasoning_delta() {
    let p = provider();
    let chunk = json!({
        "choices": [{"index": 0, "delta": {"reasoning": "thinking"}, "finish_reason": null}]
    });
    let result = p
        .transform_stream_chunk("x/y", &serde_json::to_string(&chunk).unwrap())
        .unwrap()
        .unwrap();
    let parsed: Value = serde_json::from_str(&result).unwrap();
    assert_eq!(
        parsed["choices"][0]["delta"]["reasoning"][0]["text"],
        "thinking"
    );
}

#[test]
fn stream_passes_content_delta() {
    let p = provider();
    let chunk = json!({
        "choices": [{"index": 0, "delta": {"content": "Hello"}, "finish_reason": null}]
    });
    let result = p
        .transform_stream_chunk("x/y", &serde_json::to_string(&chunk).unwrap())
        .unwrap()
        .unwrap();
    let parsed: Value = serde_json::from_str(&result).unwrap();
    assert_eq!(parsed["choices"][0]["delta"]["content"], "Hello");
}

#[test]
fn stream_skips_unparseable() {
    let p = provider();
    let result = p.transform_stream_chunk("x/y", "not json").unwrap();
    assert!(result.is_none());
}

// ============================================================
// Accounting
//
// OpenRouter reports what it actually charged for a generation, and which
// upstream served it. Both were being thrown away: the request never asked for
// the cost, and the catalog estimate overwrote it when it arrived anyway.
//
// The two fixtures are real captured responses from
// `openrouter/deepseek/deepseek-v4.1-flash` with `usage: {include: true}`.
// ============================================================

fn captured_response() -> Value {
    serde_json::from_str(include_str!("fixtures/openrouter-usage-include.json")).unwrap()
}

/// The `data:` payloads of the captured stream, `[DONE]` excluded.
fn captured_stream() -> Vec<String> {
    include_str!("fixtures/openrouter-usage-include-stream.sse")
        .lines()
        .filter_map(|line| line.strip_prefix("data: "))
        .filter(|data| *data != "[DONE]")
        .map(str::to_owned)
        .collect()
}

#[test]
fn request_never_injects_an_accounting_parameter() {
    // Measured 2026-09-22: OpenRouter returns `usage.cost` unconditionally —
    // a stream carries it with no `stream_options`, and `include: false` does
    // not suppress it. Its docs call both parameters deprecated and without
    // effect. There is therefore nothing to ask for, and injecting a no-op
    // into every request would be a passthrough inventing a parameter.
    let result = provider()
        .transform_request(
            "deepseek/deepseek-v4.1-flash",
            &json!({"messages": [{"role": "user", "content": "hi"}]}),
        )
        .unwrap();
    assert!(
        result.body.get("usage").is_none(),
        "nothing asked for accounting, so nothing should be sent: {}",
        result.body
    );
}

#[test]
fn request_forwards_an_explicit_usage_object_unchanged() {
    // A caller may still be pointing at an OpenRouter-compatible endpoint that
    // does honour the switch. Dropping their value would be the passthrough
    // bug this adapter exists to avoid — `usage` was missing from the
    // forwarded key list entirely.
    let p = provider();
    for asked in [json!({"include": false}), json!({"include": true})] {
        let result = p
            .transform_request(
                "deepseek/deepseek-v4.1-flash",
                &json!({"messages": [{"role": "user", "content": "hi"}], "usage": asked}),
            )
            .unwrap();
        assert_eq!(result.body["usage"], asked);
    }

    // `x-openrouter.usage` is the same statement in the native namespace.
    let result = p
        .transform_request(
            "deepseek/deepseek-v4.1-flash",
            &json!({
                "messages": [{"role": "user", "content": "hi"}],
                "x-openrouter": {"usage": {"include": true}},
            }),
        )
        .unwrap();
    assert_eq!(result.body["usage"], json!({"include": true}));
}

#[test]
fn response_keeps_the_reported_bill_and_who_served_the_call() {
    // `normalize_response` adds cache counters to the usage object in place, so
    // the accounting fields beside them have to survive it — and `provider` is
    // the only record of which upstream OpenRouter routed to.
    let result = provider()
        .transform_response("deepseek/deepseek-v4.1-flash", captured_response())
        .unwrap();

    assert_eq!(result["provider"], "Together");
    assert_eq!(result["id"], "gen-1790055505-SQhAOovddGuTBgM1SM6y");

    let usage = &result["usage"];
    assert_eq!(usage["cost"], 0.0000873);
    assert_eq!(usage["is_byok"], false);
    assert_eq!(usage["cost_details"]["upstream_inference_cost"], 0.0000873);
    assert_eq!(
        usage["cost_details"]["upstream_inference_prompt_cost"],
        0.0000141
    );
    // The normalized counters are computed beside them, not instead of them.
    assert_eq!(usage["cache_read_tokens"], 0);
    assert_eq!(usage["uncached_input_tokens"], 47);
    assert_eq!(usage["completion_tokens_details"]["reasoning_tokens"], 52);
}

#[test]
fn the_reported_bill_outranks_the_catalog_estimate() {
    // A catalog price is an estimate of this number, and deliberately an upper
    // bound. Letting it overwrite the number would discard the only exact
    // figure in the response.
    let mut result = provider()
        .transform_response("deepseek/deepseek-v4.1-flash", captured_response())
        .unwrap();
    llmshim::cost::stamp("openrouter", "deepseek/deepseek-v4.1-flash", &mut result);

    assert_eq!(result["usage"]["cost_usd"], 0.0000873);
    assert_eq!(result["usage"]["cost_source"], "provider");
    assert_eq!(
        result["usage"]["cost_usd"], result["usage"]["cost"],
        "the stamped cost must be the reported bill, to the last decimal"
    );
}

#[test]
fn a_reported_bill_is_believed_even_where_the_catalog_has_no_price() {
    // The whole point for an aggregator: OpenRouter carries slugs the catalog
    // has never heard of, and for those the reported bill is the *only*
    // possible answer. A catalog miss must not null it out.
    let mut result = json!({"usage": {
        "prompt_tokens": 47, "completion_tokens": 61, "uncached_input_tokens": 47,
        "cost": 0.0000873,
    }});
    llmshim::cost::stamp("openrouter", "definitely-not-in-the-catalog", &mut result);
    assert_eq!(result["usage"]["cost_usd"], 0.0000873);
    assert_eq!(result["usage"]["cost_source"], "provider");
}

#[test]
fn a_provider_that_reports_nothing_still_gets_the_catalog_path() {
    // Every other provider. The old behaviour, unchanged, now labelled.
    let mut result = json!({"usage": {
        "uncached_input_tokens": 1_000_000, "completion_tokens": 0, "prompt_tokens": 1_000_000,
    }});
    llmshim::cost::stamp("anthropic", "claude-sonnet-5", &mut result);
    assert!(
        result["usage"]["cost_usd"].as_f64().unwrap() > 0.0,
        "a catalogued model must still be priced"
    );
    assert_eq!(result["usage"]["cost_source"], "catalog");

    // And an unpriceable model still stamps null, never 0 — the source names
    // the path that answered, not the confidence of the answer.
    let mut unknown = json!({"usage": {"uncached_input_tokens": 1, "completion_tokens": 1}});
    llmshim::cost::stamp("nowhere", "definitely-not-a-model", &mut unknown);
    assert!(unknown["usage"]["cost_usd"].is_null());
    assert_eq!(unknown["usage"]["cost_source"], "catalog");
}

#[test]
fn stream_chunks_keep_the_bill_and_the_serving_upstream() {
    // The client stamps every chunk as it passes (`stamp_chunk`), so the
    // terminal chunk is where cost lands on a stream. `provider` rides on all
    // of them.
    let p = provider();
    let chunks = captured_stream();
    assert!(
        chunks.len() > 2,
        "fixture should be a real multi-chunk stream"
    );

    let mut terminal = None;
    for chunk in &chunks {
        let out = p
            .transform_stream_chunk("deepseek/deepseek-v4.1-flash", chunk)
            .unwrap()
            .expect("openrouter chunks are forwarded, not swallowed");
        let out = llmshim::cost::stamp_chunk("openrouter", "deepseek/deepseek-v4.1-flash", out);
        let parsed: Value = serde_json::from_str(&out).unwrap();
        assert_eq!(parsed["provider"], "Together");
        assert_eq!(parsed["id"], "gen-1790055506-E2fSyv9tIeHD6SXuq9A3");
        if parsed["usage"].is_object() {
            terminal = Some(parsed);
        }
    }

    let terminal = terminal.expect("the captured stream carries usage on its last chunk");
    assert_eq!(terminal["usage"]["cost"], 0.0001734);
    assert_eq!(terminal["usage"]["cost_usd"], 0.0001734);
    assert_eq!(terminal["usage"]["cost_source"], "provider");
    assert_eq!(terminal["usage"]["is_byok"], false);
    assert_eq!(
        terminal["usage"]["cost_details"]["upstream_inference_completions_cost"],
        0.0001572
    );
    assert_eq!(terminal["usage"]["cache_read_tokens"], 0);
}

#[tokio::test]
async fn a_buffered_response_still_names_the_upstream_that_served_it() {
    // `shim::collect` rebuilds one response out of the chunks for every
    // buffered path (managed tool protocols, structured output). It copied a
    // fixed field list that omitted `provider`, so the aggregator's statement
    // of who served the call was dropped exactly where a repair might need it.
    let chunks: Vec<_> = captured_stream().into_iter().map(Ok).collect();
    let collected = llmshim::shim::collect(Box::pin(futures::stream::iter(chunks)))
        .await
        .unwrap();

    assert_eq!(collected["provider"], "Together");
    assert_eq!(collected["id"], "gen-1790055506-E2fSyv9tIeHD6SXuq9A3");
    assert_eq!(collected["usage"]["cost"], 0.0001734);
}

#[test]
fn a_log_entry_says_which_of_the_two_numbers_it_carries() {
    // Reading spend off a JSONL log means knowing whether the figure is an
    // invoice or an estimate.
    let mut result = provider()
        .transform_response("deepseek/deepseek-v4.1-flash", captured_response())
        .unwrap();
    llmshim::cost::stamp("openrouter", "deepseek/deepseek-v4.1-flash", &mut result);
    let entry = llmshim::log::LogEntry::from_response(
        "openrouter",
        "deepseek/deepseek-v4.1-flash",
        &result,
        std::time::Duration::from_millis(1),
    );
    assert_eq!(entry.cost_usd, Some(0.0000873));
    assert_eq!(entry.cost_source.as_deref(), Some("provider"));
    assert_eq!(
        entry.request_id.as_deref(),
        Some("gen-1790055505-SQhAOovddGuTBgM1SM6y")
    );
}