Title: feat(forge-ai): cost controls with budget enforcement, response cache, and usage ledger · Issue #90 · elcode-id/elcode-forge · GitHub
Open Graph Title: feat(forge-ai): cost controls with budget enforcement, response cache, and usage ledger · Issue #90 · elcode-id/elcode-forge
X Title: feat(forge-ai): cost controls with budget enforcement, response cache, and usage ledger · Issue #90 · elcode-id/elcode-forge
Description: 1. Context & Objective Implement cost controls in crates/forge-ai: preflight token cap checks, BLAKE3-keyed response caching in .eforge/ai-cache/
Open Graph Description: 1. Context & Objective Implement cost controls in crates/forge-ai: preflight token cap checks, BLAKE3-keyed response caching in .eforge/ai-cache/
X Description: 1. Context & Objective Implement cost controls in crates/forge-ai: preflight token cap checks, BLAKE3-keyed response caching in .eforge/ai-cache/<hash>.json, append-only usage ledger in ....
Opengraph URL: https://github.com/elcode-id/elcode-forge/issues/90
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"feat(forge-ai): cost controls with budget enforcement, response cache, and usage ledger","articleBody":"## 1. Context & Objective\nImplement cost controls in `crates/forge-ai`: preflight token cap checks, BLAKE3-keyed response caching in `.eforge/ai-cache/\u003chash>.json`, append-only usage ledger in `.eforge/ai-usage.jsonl`, and daily USD budget enforcement via `ForgeError::AiBudgetExceeded` (PRD §20, §36).\n\n- **Parent Epic:** #83\n- **GitFlow Branch:** `feat/forge-090-ai-cost-controls`\n- **Execution Wave:** `wave-11`\n- **DAG Dependencies:** Depends on #86\n- **Estimated Size:** ~290 LOC (`\u003c 400 LOC`)\n\n## 2. Target Files\n- `crates/forge-ai/src/budget.rs` *(NEW: `preflight_check`, `enforce_daily_budget`, `compute_cost_usd`)*\n- `crates/forge-ai/src/cache.rs` *(NEW: `ResponseCache` under `.eforge/ai-cache/`)*\n- `crates/forge-ai/src/usage.rs` *(NEW: `UsageRecord`, `UsageSummary`, `append_usage`, `summarize_usage`)*\n- `crates/forge-ai/src/lib.rs` *(MODIFY)*\n\n## 3. Public Contract\n```rust\npub fn preflight_check(pack: &ContextPack, cfg: &AiConfig) -> Result\u003c(), ForgeError>;\n\npub struct ResponseCache { /* .eforge/ai-cache/\u003cblake3>.json */ }\nimpl ResponseCache {\n pub fn new(repo_root: &Path) -> Self;\n pub fn key(provider: &str, model: &str, system: &str, payload: &str, schema: Option\u003c&str>) -> String;\n pub fn get(&self, key: &str) -> Result\u003cOption\u003cserde_json::Value>, ForgeError>;\n pub fn put(&self, key: &str, value: &serde_json::Value) -> Result\u003c(), ForgeError>;\n}\n\npub struct UsageRecord {\n pub date: String,\n pub provider: String,\n pub model: String,\n pub command: String,\n pub input_tokens: u32,\n pub output_tokens: u32,\n pub cost_usd: Option\u003cf64>,\n pub cached: bool,\n}\n\npub fn append_usage(repo_root: &Path, rec: &UsageRecord) -> Result\u003c(), ForgeError>;\npub fn summarize_usage(repo_root: &Path, today: &str) -> Result\u003cUsageSummary, ForgeError>;\npub fn enforce_daily_budget(summary: &UsageSummary, cfg: &AiConfig) -> Result\u003c(), ForgeError>;\n```\n\n## 4. Security & Privacy Invariants\n- Cache files in `.eforge/ai-cache/` NEVER store API keys or HTTP headers.\n- Cache keys validate strictly as 64-char hex BLAKE3 digests to prevent path traversal outside `.eforge/ai-cache/`.\n- Corrupted lines in `.eforge/ai-usage.jsonl` are skipped safely without panicking.\n\n## 5. Definition of Done & Verification (Tier 1)\n- Unit tests for `preflight_check`, deterministic BLAKE3 cache key generation, cache hit/miss persistence, `summarize_usage` with injected `today` date, `enforce_daily_budget` returning `ForgeError::AiBudgetExceeded`, and `cost_usd: None` when pricing is unconfigured.\n- **Selective Verification Gate (Tier 1):**\n 1. `cargo fmt --package forge-ai -- --check`\n 2. `cargo clippy --package forge-ai --all-targets -- -D warnings`\n 3. `cargo test --package forge-ai`","author":{"url":"https://github.com/luksluki","@type":"Person","name":"luksluki"},"datePublished":"2026-10-19T14:22:27Z","url":"https://github.com/elcode-id/elcode-forge/issues/90"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a2587f75-422a-89cc-ae83-119f651a78f9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BEB4:15653A:160869:1E30F8:6B059815 |
| html-safe-nonce | 98c64d58146212286d6070dd21f7d6774f26b02ca65bce93a3c6ecd5931b6946 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRUI0OjE1NjUzQToxNjA4Njk6MUUzMEY4OjZCMDU5ODE1IiwidmlzaXRvcl9pZCI6IjM5NTYyODgzMTczODkzNzk2MDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 48374bcc6fe38c44c09cca9b3e44ec443464404f7e5def298b8986e93f21979e |
| hovercard-subject-tag | issue:5925665876 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/elcode-id/elcode-forge/90/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d93dc2003d5bee7c1fd82281ceb9ca16457fe7ec31433cb23f0de45bfbcceb3e/elcode-id/elcode-forge/issues/90 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d93dc2003d5bee7c1fd82281ceb9ca16457fe7ec31433cb23f0de45bfbcceb3e/elcode-id/elcode-forge/issues/90 |
| og:image:alt | 1. Context & Objective Implement cost controls in crates/forge-ai: preflight token cap checks, BLAKE3-keyed response caching in .eforge/ai-cache/ |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | luksluki |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3b72fd2385d76a50ce0fe937d50bb4a81434432c4fd47866074a26f6cd03623f |
| turbo-cache-control | no-preview |
| go-import | github.com/elcode-id/elcode-forge git https://github.com/elcode-id/elcode-forge.git |
| octolytics-dimension-user_id | 337957755 |
| octolytics-dimension-user_login | elcode-id |
| octolytics-dimension-repository_id | 1423983661 |
| octolytics-dimension-repository_nwo | elcode-id/elcode-forge |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1423983661 |
| octolytics-dimension-repository_network_root_nwo | elcode-id/elcode-forge |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 58e2bd32336f3dcee343ec627a4d09c7c444e214 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width