budget

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package budget provides agent-side enforcement of rolling 24-hour token budgets.

The operator injects AGENT_DAILY_TOKEN_LIMIT into each agent pod. The agent checks this limit before running every task using a Redis sorted set as the shared counter. This is proactive enforcement — the task is rejected before any LLM call is made, not after the operator notices the overage on its next reconcile cycle.

Redis key layout:

ark:budget:{namespace}:{agentName}:usage
  score  = Unix milliseconds of task completion time
  member = "{taskID}:{totalTokens}"

Entries older than 24 hours are pruned on every Check call.

Index

Constants

This section is empty.

Variables

View Source
var ErrBudgetExceeded = errors.New("daily token budget exceeded")

ErrBudgetExceeded is returned by Check when the rolling 24h token budget is full.

Functions

This section is empty.

Types

type Store

type Store interface {
	// Check returns ErrBudgetExceeded if the daily token limit has been reached.
	// It also prunes entries older than 24 hours from the underlying store.
	Check(ctx context.Context) error

	// Record persists token usage for a completed task so it counts toward the budget.
	Record(ctx context.Context, taskID string, totalTokens int64) error

	// Close releases any resources held by the store.
	Close() error
}

Store checks and records token usage against a rolling 24-hour budget.

func NewStore

func NewStore(redisURL string, limit int64, namespace, agentName string) (Store, error)

NewStore returns a Store that enforces the given daily token limit. Returns a no-op store when limit <= 0 or when namespace/agentName are empty (local development, ark run, or no limit configured).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL