Skip to main content
Version: 1.0.0

Test a monetary budget without paid calls

This exercise checks the SDK ledger without a model, API key or invoice. Amounts form a test contract, not a provider tariff. Install the JavaScript SDK or Python SDK.

JavaScript​

Save this program as budget-test.mjs and run node budget-test.mjs. The temporary directory belongs to this exercise.

import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createSqliteProviderBudget } from "@touba/sdk/budget/sqlite";

const directory = mkdtempSync(join(tmpdir(), "touba-budget-example-"));
const options = { filename: join(directory, "costs.sqlite"),
budgetId: "test-operation", currency: "USD", limitMicros: 100n };
const quote = { basis: "upper_bound", maxCostMicros: 60n, currency: "USD",
tariffId: "exercise-contract", tariffDate: "2026-10-18" };
let budget;
try {
budget = createSqliteProviderBudget(options);
const attempt = budget.reserve(quote);
budget.settle(attempt, { kind: "unknown" });
assert.equal(budget.snapshot().remainingMicros, 40n);
assert.throws(() => budget.reserve(quote),
error => error.code === "ProviderCostBudgetExceeded");
budget.close();
budget = createSqliteProviderBudget(options);
assert.equal(budget.snapshot().remainingMicros, 40n);
assert.equal(budget.listAttempts({ status: "unknown" })[0].id, attempt);
// Test evidence only; production reconciliation requires provider evidence.
budget.settle(attempt, { kind: "reported", costMicros: 20n });
assert.equal(budget.snapshot().remainingMicros, 80n);
console.log("BUDGET_OK");
} finally {
budget?.close();
rmSync(directory, { recursive: true, force: true });
}

Python​

Save this program as budget_test.py and run it with the project Python where the SDK is installed. It checks exactly the same cycle.

from pathlib import Path
from tempfile import TemporaryDirectory
from touba_sdk import ToubaAgentDecisionProviderError
from touba_sdk.budget.sqlite import create_sqlite_provider_budget

with TemporaryDirectory(prefix="touba-budget-example-") as directory:
options = dict(filename=Path(directory) / "costs.sqlite",
budget_id="test-operation", currency="USD", limit_micros=100)
quote = dict(basis="upper_bound", max_cost_micros=60, currency="USD",
tariff_id="exercise-contract", tariff_date="2026-10-18")
budget = create_sqlite_provider_budget(**options)
try:
attempt = budget.reserve(quote)
budget.settle(attempt, {"kind": "unknown"})
assert budget.snapshot()["remaining_micros"] == 40
try:
budget.reserve(quote)
except ToubaAgentDecisionProviderError as error:
assert error.code == "ProviderCostBudgetExceeded"
else:
raise AssertionError("Expected a monetary budget refusal")
budget.close()
budget = create_sqlite_provider_budget(**options)
assert budget.snapshot()["remaining_micros"] == 40
assert budget.list_attempts(status="unknown")[0]["id"] == attempt
# Test evidence only; production reconciliation requires provider evidence.
budget.settle(attempt, {"kind": "reported", "cost_micros": 20})
assert budget.snapshot()["remaining_micros"] == 80
print("BUDGET_OK")
finally:
budget.close()

Read the result​

BUDGET_OK confirms that unknown usage retains its reservation, overspending is refused, and reopening preserves the balance. Only explicit usage evidence then releases unused funds. This tests accounting, not an LLM call or tariff accuracy. To protect real requests, follow the provider cost guide.