Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(cost): add identity cost config and budget tracking

Steven Enamakel6db5b5da6784
diff --git a/crates/openhuman-core/src/config/schema/identity_cost.rs b/crates/openhuman-core/src/config/schema/identity_cost.rs
index 98771f47f0..77931873be 100644
--- a/crates/openhuman-core/src/config/schema/identity_cost.rs
+++ b/crates/openhuman-core/src/config/schema/identity_cost.rs
@@ -63,7 +63,7 @@ pub struct CostConfig {
}
/// One budget: a limit on spend and/or tokens over a period, for every call
-/// or for each thread, agent, model, provider or user agent.
+/// or for each thread, agent, model or user agent.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct BudgetPolicy {
@@ -105,7 +105,6 @@ pub enum BudgetScope {
/// The agent definition making the call.
Agent,
Model,
- Provider,
/// The embedded or SaaS user agent.
SessionAgent,
}
diff --git a/crates/openhuman-core/src/platform/cost/README.md b/crates/openhuman-core/src/platform/cost/README.md
index ea88de45f8..06fc8b7990 100644
--- a/crates/openhuman-core/src/platform/cost/README.md
+++ b/crates/openhuman-core/src/platform/cost/README.md
@@ -27,7 +27,7 @@ Local API-usage cost tracking for the agent. Records per-call token usage and co
| `crates/openhuman-core/src/platform/cost/route.rs` | `CostRoute` / `route_for_model`: derives from the model id whether a record counts against OpenHuman-managed credits or is BYOK/local (#5016). |
| `crates/openhuman-core/src/platform/cost/scope.rs` | `UsageScope::ambient`: a record's attribution (thread, origin, agent definition, sub-agent task, embedded/SaaS user agent, provider) read from the recording task's turn origin, memory identity and `CoreContext`. |
| `crates/openhuman-core/src/platform/cost/report.rs` | Pure usage reports over ledger records: `build_report` (group by day/week/month/model/provider/route/agent/thread/origin/session_agent; tokens, charged vs estimated cost, cache-hit ratio) and `build_cache_report` (per-call hits, cold calls, uncached premium). |
-| `crates/openhuman-core/src/platform/cost/budget.rs` | Budgets (`[[cost.budgets]]`): `evaluate` / `check_call` sum a policy's bucket (global, or this call's thread/agent/model/provider/user agent) over its day or month and report warnings and refusals. The agent budget gate (`agent/tinyagents/host/budget_gate.rs`) runs it before every model call. |
+| `crates/openhuman-core/src/platform/cost/budget.rs` | Budgets (`[[cost.budgets]]`): `evaluate` / `check_call` sum a policy's bucket (global, or this call's thread/agent/model/user agent) over its day or month and report warnings and refusals. The agent budget gate (`agent/tinyagents/host/budget_gate.rs`) runs it before every model call. |
| `crates/openhuman-core/src/platform/cost/tools.rs` | Read-only, default-on LLM tools (`cost_get_dashboard`, `cost_get_daily_history`, …) re-exported through `crates/openhuman-core/src/tools/mod.rs`. |
## Public surface
@@ -90,7 +90,7 @@ None. The module has no `bus.rs` and no `DomainEvent` publishers/subscribers.
## Notes / gotchas
-- **`cost.enabled` gates `record_usage`, not telemetry.** The agent path uses `record_usage_unconditional`, so the local usage ledger continues to grow when this flag is false. The core no longer enforces a cost cap; hosted-credit exhaustion is enforced by the backend.
+- **`cost.enabled` gates `record_usage`, not telemetry.** The agent path uses `record_usage_unconditional`, so the local usage ledger continues to grow when this flag is false. The legacy `monthly_limit_usd` only drives the dashboard; the only caps the core enforces are the opt-in `[[cost.budgets]]` below. Hosted-credit exhaustion is enforced by the backend.
- The global tracker is a one-shot `OnceCell`; `init_global` is idempotent and never panics on construction failure (it logs and leaves `try_global() == None`). Callers before bootstrap (e.g. unit tests) must treat the absence as a soft no-op.
- `record_provider_usage` skips all-zero `UsageInfo` payloads (`input==0 && output==0 && charged==0.0`) so providers that don't echo usage don't inflate the request count.
- Provider-charged USD is persisted directly with `cost_source = provider_charged`; otherwise usage remains `estimated`. Cached input tokens are clamped to `input_tokens` during provider usage translation.
@@ -112,7 +112,7 @@ action = "refuse" # or "warn" (default)
[[cost.budgets]]
name = "planner per day"
-scope = "agent" # global (default) | thread | agent | model | provider | session_agent
+scope = "agent" # global (default) | thread | agent | model | session_agent
match = "planner" # omit to apply to each value separately
period = "day"
max_usd = 2.0
diff --git a/crates/openhuman-core/src/platform/cost/budget.rs b/crates/openhuman-core/src/platform/cost/budget.rs
index 713c127ae5..83be26e0dd 100644
--- a/crates/openhuman-core/src/platform/cost/budget.rs
+++ b/crates/openhuman-core/src/platform/cost/budget.rs
@@ -4,8 +4,7 @@
//! [`evaluate`] is a pure function of the policies, the ledger records of the
//! period and the call about to be made, so every rule is testable without a
//! ledger. For each policy it works out which bucket the call falls in — all
-//! calls (`global`), or this call's thread, agent, model, provider or user
-//! agent — sums that bucket's spend and tokens over the period, and compares
+//! calls (`global`), or this call's thread, agent, model or user agent — sums that bucket's spend and tokens over the period, and compares
//! them with the limits:
//!
//! - at or over a limit, a `refuse` policy refuses the call and a `warn`
@@ -105,7 +104,6 @@ fn bucket_of<'a>(scope: BudgetScope, model: &'a str, usage: &'a UsageScope) -> O
BudgetScope::Model => Some(model),
BudgetScope::Thread => usage.thread_id.as_deref(),
BudgetScope::Agent => usage.agent_id.as_deref(),
- BudgetScope::Provider => usage.provider.as_deref(),
BudgetScope::SessionAgent => usage.session_agent.as_deref(),
}
}