Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Budget-aware multi-model routing with per-task cost tracking

オープン
#598 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
28/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
rust
領域
ai, api, backend

調査の方向性

Start in the affected terraphim_service, terraphim_types, terraphim_multi_agent, and terraphim_mcp_server crates, then review the event-sourcing dependency in #597 and the existing terraphim-llm-proxy token headers. Done means task budgets meter costs, enforce limits, fall back through model chains, persist cost events, and expose the current and historical MCP budget resources.

索引モデルが issue の本文から書いたものです。

説明

architecture enhancement

Summary

Add per-task cost tracking with fallback chains and hard spending limits to LLM routing. Each agent operation gets a budget allocation; if the primary model is too expensive or unavailable, fall back to cheaper alternatives automatically.

Motivation

Inspired by Ouroboros per-round cost tracking with per-worker budget allocation. Current state:

  • terraphim-llm-proxy routes between multiple models but has no per-task budget tracking
  • genai (terraphim fork) handles LLM calls but does not meter usage or cost
  • The multi-model judge pattern (quick -> deep -> tiebreaker) needs budget awareness: only escalate to expensive models when cheaper ones disagree
  • No spending limits exist -- a runaway agent loop could consume unbounded API credits

Implementation Plan

1. Budget Struct
pub struct TaskBudget {
    pub max_cost_usd: f64,
    pub model_chain: Vec<ModelConfig>,  // ordered by preference (cheapest first)
    pub spent_usd: f64,
    pub request_count: u32,
    pub created_at: jiff::Timestamp,
}

pub struct ModelConfig {
    pub model_id: String,
    pub cost_per_1k_input: f64,
    pub cost_per_1k_output: f64,
    pub max_latency_ms: u64,
    pub fallback_on: Vec<FallbackReason>,  // Timeout, RateLimit, BudgetExceeded, EmptyResponse
}
2. Cost Tracking in terraphim_service
  • After each LLM call, compute cost from token counts + model pricing
  • Accumulate in TaskBudget.spent_usd
  • If spent_usd >= max_cost_usd, reject further calls for this task
  • Log cost events to event store (#597)
3. Fallback Chain Logic
  • Try models in model_chain order
  • On failure (timeout, rate limit, empty response, budget exceeded for this model tier), try next model
  • If all models exhausted, return error with cost summary
4. Integration with terraphim-llm-proxy
  • Proxy already reports token counts in responses
  • Add cost metadata to proxy response headers (cost_usd, model_used, tokens_in, tokens_out)
  • Agent-side budget tracking consumes these headers
5. MCP Resource Exposure
  • Expose /budget/current as MCP resource showing active task budgets
  • Expose /budget/history for cost analysis over time

Affected Crates

  • terraphim_service (primary -- add budget tracking to LLM routing)
  • terraphim_types (add TaskBudget, ModelConfig types)
  • terraphim_multi_agent (wire budget into agent task execution)
  • terraphim_mcp_server (expose budget resources)

Dependencies

  • #597 Event sourcing (for cost event persistence)

Estimated Effort

~4 hours (mostly plumbing between terraphim_service and proxy response headers)

Part of

Epic #595

主要言語
Rust
スター
62
フォーク
5
平均マージ
2時間 27分
マージ済み PR(30日)
1

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

terraphim/terraphim-ai のほかの issue

terraphim/terraphim-ai の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。