Skip to content

Commit ab95bab

Browse files
authored
build(pricing): stop tracking LiteLLM fallback JSON (#1132)
* build(pricing): stop tracking LiteLLM fallback JSON Remove the checked-in LiteLLM pricing fallback snapshot and the Nix helper that regenerated it. Nix builds still embed pricing from the locked litellm flake input via CCUSAGE_PRICING_JSON_PATH, while non-Nix Cargo builds now fetch LiteLLM pricing directly during build and fail if that fetch or compacting step fails. Keep the build-time compaction in build.rs so the embedded snapshot still only includes model prefixes and fields used by pricing. The scheduled pricing workflow continues to update the litellm flake input, but now commits only flake.lock instead of a generated repository JSON file. * build(pricing): fetch locked LiteLLM revisions Make non-Nix Cargo builds read the litellm revision from flake.lock before fetching model_prices_and_context_window.json. This keeps embedded pricing aligned with the Nix build path without checking a generated pricing snapshot into the repository. Document the locked-revision flow in the package README and the ccusage Rust skill so future pricing changes preserve the same source of truth. * fix(pricing): return flake lock field errors Make LiteLLM flake.lock field extraction return InvalidData errors instead of reusing the CLI help helper that panics on missing or non-string fields. This keeps non-Nix Cargo builds on the locked LiteLLM revision while preserving build.rs error handling when flake.lock is malformed.
1 parent aa8e101 commit ab95bab

8 files changed

Lines changed: 70 additions & 2425 deletions

File tree

‎.agents/skills/ccusage-rust/SKILL.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,16 @@ TypeScript uses build/macro-time pricing snapshots. Rust should not rely on a ma
5252

5353
When changing pricing:
5454

55-
- Prefer a `build.rs` step that fetches LiteLLM `model_prices_and_context_window.json` into `OUT_DIR`.
56-
- Keep a checked-in fallback snapshot so offline builds and network failures still work.
57-
- Load the generated build-time snapshot first, then fallback pricing, then runtime fetch when not `--offline`.
55+
- Use the `litellm` flake input as the canonical pinned pricing revision for
56+
embedded pricing.
57+
- For Nix builds, pass the locked LiteLLM `model_prices_and_context_window.json`
58+
to `build.rs` through `CCUSAGE_PRICING_JSON_PATH`.
59+
- For non-Nix Cargo builds, have `build.rs` read the same `litellm` revision from
60+
`flake.lock` and fetch that pinned raw JSON at build time.
61+
- Do not check generated LiteLLM pricing snapshots into the repository.
62+
- Keep pricing JSON filtering and compacting in `build.rs` so runtime code loads
63+
the generated build-time snapshot first, then built-in model overrides, then
64+
runtime fetch when not `--offline`.
5865
- Add tests for embedded/offline pricing and context limits.
5966

6067
## Validation

‎.github/workflows/update-pricing.yaml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ jobs:
1818
- name: Update LiteLLM pricing input
1919
run: |
2020
nix flake update litellm
21-
nix run .#update-pricing-fallback
2221
nix flake check
2322
- name: Create pull request
2423
env:
@@ -34,12 +33,12 @@ jobs:
3433
3534
branch="automation/litellm-pricing"
3635
git checkout -B "$branch"
37-
git add flake.lock rust/crates/ccusage/src/litellm-pricing-fallback.json
36+
git add flake.lock
3837
git commit -m "chore(pricing): update LiteLLM snapshot"
3938
git push --force-with-lease origin "$branch"
4039
4140
body="$(printf '%s\n' \
42-
'Updates the locked LiteLLM pricing input and refreshes the non-Nix Cargo fallback pricing JSON.' \
41+
'Updates the locked LiteLLM pricing input used by Nix builds.' \
4342
'' \
4443
'Validation:' \
4544
'- nix flake check')"

‎apps/ccusage/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,12 @@ nix run github:ryoppippi/ccusage -- codex daily --offline
188188
nix build github:ryoppippi/ccusage
189189
```
190190

191-
Nix builds embed the LiteLLM pricing file from the locked `litellm` flake input, so sandboxed builds do not fetch pricing at build time. To update the locked pricing snapshot and refresh the non-Nix Cargo fallback JSON:
191+
Nix builds embed the LiteLLM pricing file from the locked `litellm` flake input, so sandboxed builds do not fetch pricing at build time. To update the locked pricing snapshot:
192+
193+
Non-Nix Cargo builds read the same locked LiteLLM revision from `flake.lock` and fetch the pricing file from that revision at build time.
192194

193195
```bash
194196
nix flake update litellm
195-
nix run .#update-pricing-fallback
196197
nix flake check
197198
```
198199

‎flake.nix‎

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
type = "app";
4949
program = package;
5050
};
51-
update-pricing-fallback = {
52-
type = "app";
53-
program = nixpkgs.lib.getExe self.packages.${system}.update-pricing-fallback;
54-
};
5551
});
5652

5753
packages = forAllSystems (system: pkgs:
@@ -64,8 +60,7 @@
6460
filter = path: type:
6561
(craneLib.filterCargoSources path type)
6662
|| pkgs.lib.hasSuffix "/cli-help.json" path
67-
|| pkgs.lib.hasSuffix "/fast-multiplier-overrides.json" path
68-
|| pkgs.lib.hasSuffix "/litellm-pricing-fallback.json" path;
63+
|| pkgs.lib.hasSuffix "/fast-multiplier-overrides.json" path;
6964
};
7065
repoSrc = mkRepoSrc pkgs;
7166
commonArgs = {
@@ -140,33 +135,9 @@
140135
});
141136
}
142137
);
143-
update-pricing-fallback = pkgs.writeShellApplication {
144-
name = "update-pricing-fallback";
145-
runtimeInputs = with pkgs; [ coreutils git jq oxfmt ];
146-
text = ''
147-
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
148-
target="$repo_root/rust/crates/ccusage/src/litellm-pricing-fallback.json"
149-
if [ ! -f "$target" ]; then
150-
echo "fallback pricing file not found: $target" >&2
151-
exit 1
152-
fi
153-
tmp="$(mktemp "$repo_root/rust/crates/ccusage/src/litellm-pricing-fallback.XXXXXX.json")"
154-
jq --tab -f ${./nix/pricing-fallback.jq} ${litellm-pricing} > "$tmp"
155-
oxfmt --config ${./.oxfmtrc.json} --write "$tmp"
156-
mv "$tmp" "$target"
157-
'';
158-
};
159-
pricing-fallback-sync = pkgs.runCommand "pricing-fallback-sync" {
160-
nativeBuildInputs = with pkgs; [ diffutils jq oxfmt ];
161-
} ''
162-
jq --tab -f ${./nix/pricing-fallback.jq} ${litellm-pricing} > generated.json
163-
oxfmt --config ${./.oxfmtrc.json} --write generated.json
164-
diff -u ${./rust/crates/ccusage/src/litellm-pricing-fallback.json} generated.json
165-
touch $out
166-
'';
167138
in {
168139
default = ccusage;
169-
inherit ccusage ccusage-clippy ccusage-fmt pricing-fallback-sync update-pricing-fallback;
140+
inherit ccusage ccusage-clippy ccusage-fmt;
170141
} // staticCcusage);
171142

172143
checks = forAllSystems (system: pkgs:
@@ -187,7 +158,6 @@
187158
inherit (self.packages.${system}) ccusage;
188159
inherit (self.packages.${system}) ccusage-clippy;
189160
inherit (self.packages.${system}) ccusage-fmt;
190-
inherit (self.packages.${system}) pricing-fallback-sync;
191161
oxfmt = mkRepoCheck "oxfmt-check" [ pkgs.oxfmt ] ''
192162
oxfmt --check .
193163
'';

‎nix/pricing-fallback.jq‎

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎rust/crates/ccusage/build.rs‎

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ use std::{env, fs, path::PathBuf};
33
use serde_json::{Map, Value};
44

55
const CLI_HELP_JSON: &str = "src/cli-help.json";
6-
const FALLBACK_PRICING_JSON: &str = "src/litellm-pricing-fallback.json";
7-
const LITELLM_PRICING_URL: &str =
8-
"https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
6+
const FLAKE_LOCK_JSON: &str = "../../../flake.lock";
7+
const LITELLM_PRICING_JSON: &str = "model_prices_and_context_window.json";
98
const OUT_PRICING_JSON: &str = "litellm-pricing.json";
109
const PRICING_JSON_PATH_ENV: &str = "CCUSAGE_PRICING_JSON_PATH";
1110
const PRICING_FETCH_TIMEOUT_SECONDS: u64 = 10;
1211

1312
fn main() {
1413
println!("cargo:rerun-if-changed={CLI_HELP_JSON}");
15-
println!("cargo:rerun-if-changed={FALLBACK_PRICING_JSON}");
1614
println!("cargo:rerun-if-env-changed={PRICING_JSON_PATH_ENV}");
17-
println!("cargo:rerun-if-env-changed=CCUSAGE_SKIP_PRICING_FETCH");
1815

1916
generate_cli_help_rs();
2017

@@ -23,15 +20,11 @@ fn main() {
2320
let path = PathBuf::from(path);
2421
println!("cargo:rerun-if-changed={}", path.display());
2522
fs::read_to_string(path).expect("read pricing snapshot from CCUSAGE_PRICING_JSON_PATH")
26-
} else if env::var_os("CCUSAGE_SKIP_PRICING_FETCH").is_some() {
27-
fallback_pricing_json()
2823
} else {
29-
fetch_pricing_json().unwrap_or_else(|error| {
30-
println!("cargo:warning=failed to fetch LiteLLM pricing for embed: {error}");
31-
fallback_pricing_json()
32-
})
24+
println!("cargo:rerun-if-changed={FLAKE_LOCK_JSON}");
25+
fetch_pricing_json().expect("fetch LiteLLM pricing for embed")
3326
};
34-
let pricing_json = compact_pricing_json(&pricing_json).unwrap_or(pricing_json);
27+
let pricing_json = compact_pricing_json(&pricing_json).expect("compact LiteLLM pricing JSON");
3528

3629
fs::write(out_path, pricing_json).expect("write build-time pricing snapshot");
3730
}
@@ -134,7 +127,7 @@ fn rust_string_literal(value: &str) -> String {
134127
}
135128

136129
fn fetch_pricing_json() -> std::io::Result<String> {
137-
let response = minreq::get(LITELLM_PRICING_URL)
130+
let response = minreq::get(litellm_pricing_url()?)
138131
.with_timeout(PRICING_FETCH_TIMEOUT_SECONDS)
139132
.send()
140133
.map_err(|error| std::io::Error::other(error.to_string()))?;
@@ -150,8 +143,50 @@ fn fetch_pricing_json() -> std::io::Result<String> {
150143
.to_string())
151144
}
152145

153-
fn fallback_pricing_json() -> String {
154-
fs::read_to_string(FALLBACK_PRICING_JSON).expect("read fallback pricing snapshot")
146+
fn litellm_pricing_url() -> std::io::Result<String> {
147+
let flake_lock = fs::read_to_string(FLAKE_LOCK_JSON)?;
148+
let Value::Object(root) = serde_json::from_str::<Value>(&flake_lock)
149+
.map_err(|error| std::io::Error::new(std::io::ErrorKind::InvalidData, error.to_string()))?
150+
else {
151+
return Err(std::io::Error::new(
152+
std::io::ErrorKind::InvalidData,
153+
"flake.lock must be a JSON object",
154+
));
155+
};
156+
let locked = root
157+
.get("nodes")
158+
.and_then(|nodes| nodes.get("litellm"))
159+
.and_then(|litellm| litellm.get("locked"))
160+
.and_then(Value::as_object)
161+
.ok_or_else(|| {
162+
std::io::Error::new(
163+
std::io::ErrorKind::InvalidData,
164+
"flake.lock is missing nodes.litellm.locked",
165+
)
166+
})?;
167+
let owner = required_flake_lock_string_field(locked, "owner")?;
168+
let repo = required_flake_lock_string_field(locked, "repo")?;
169+
let rev = required_flake_lock_string_field(locked, "rev")?;
170+
171+
Ok(format!(
172+
"https://raw.githubusercontent.com/{owner}/{repo}/{rev}/{LITELLM_PRICING_JSON}"
173+
))
174+
}
175+
176+
fn required_flake_lock_string_field(
177+
object: &Map<String, Value>,
178+
field: &str,
179+
) -> std::io::Result<String> {
180+
object
181+
.get(field)
182+
.and_then(Value::as_str)
183+
.map(str::to_string)
184+
.ok_or_else(|| {
185+
std::io::Error::new(
186+
std::io::ErrorKind::InvalidData,
187+
format!("flake.lock nodes.litellm.locked.{field} must be a string"),
188+
)
189+
})
155190
}
156191

157192
fn compact_pricing_json(json: &str) -> Option<String> {

0 commit comments

Comments
 (0)