@aumos/agent-energy-budget 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +121 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +129 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +278 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/package.json +36 -0
- package/src/client.ts +288 -0
- package/src/index.ts +34 -0
- package/src/types.ts +354 -0
- package/tsconfig.json +25 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client for the agent-energy-budget service API.
|
|
3
|
+
*
|
|
4
|
+
* Uses the Fetch API (available natively in Node 18+, browsers, and Deno).
|
|
5
|
+
* No external dependencies required.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { createAgentEnergyBudgetClient } from "@aumos/agent-energy-budget";
|
|
10
|
+
*
|
|
11
|
+
* const client = createAgentEnergyBudgetClient({ baseUrl: "http://localhost:8095" });
|
|
12
|
+
*
|
|
13
|
+
* // Check current budget
|
|
14
|
+
* const budget = await client.getBudget("my-agent");
|
|
15
|
+
* if (budget.ok) {
|
|
16
|
+
* console.log("Daily limit:", budget.data.daily_limit, "USD");
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Record a completed LLM call
|
|
20
|
+
* await client.trackCost({
|
|
21
|
+
* agent_id: "my-agent",
|
|
22
|
+
* model: "claude-haiku-4",
|
|
23
|
+
* input_tokens: 2048,
|
|
24
|
+
* output_tokens: 512,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Predict cost before making an LLM call
|
|
28
|
+
* const forecast = await client.predictCost({
|
|
29
|
+
* agent_id: "my-agent",
|
|
30
|
+
* model: "gpt-4o-mini",
|
|
31
|
+
* prompt_text: "Summarize the report.",
|
|
32
|
+
* max_output_tokens: 256,
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
import type { ApiResult, Budget, CostEntry, ModelPricing, PredictCostRequest, SetBudgetLimitRequest, TokenUsage, UsageReport, WorkloadForecast } from "./types.js";
|
|
37
|
+
/** Configuration options for the AgentEnergyBudgetClient. */
|
|
38
|
+
export interface AgentEnergyBudgetClientConfig {
|
|
39
|
+
/** Base URL of the agent-energy-budget server (e.g. "http://localhost:8095"). */
|
|
40
|
+
readonly baseUrl: string;
|
|
41
|
+
/** Optional request timeout in milliseconds (default: 30000). */
|
|
42
|
+
readonly timeoutMs?: number;
|
|
43
|
+
/** Optional extra HTTP headers sent with every request. */
|
|
44
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
45
|
+
}
|
|
46
|
+
/** Typed HTTP client for the agent-energy-budget service. */
|
|
47
|
+
export interface AgentEnergyBudgetClient {
|
|
48
|
+
/**
|
|
49
|
+
* Retrieve the current budget configuration for an agent.
|
|
50
|
+
*
|
|
51
|
+
* Returns daily, weekly, and monthly limits alongside the configured
|
|
52
|
+
* degradation strategy, alert thresholds, and model preferences.
|
|
53
|
+
*
|
|
54
|
+
* @param agentId - The unique agent identifier.
|
|
55
|
+
* @returns The Budget configuration record for this agent.
|
|
56
|
+
*/
|
|
57
|
+
getBudget(agentId: string): Promise<ApiResult<Budget>>;
|
|
58
|
+
/**
|
|
59
|
+
* Record the actual cost of a completed LLM call.
|
|
60
|
+
*
|
|
61
|
+
* Persists the call record to JSONL storage and fires any alert
|
|
62
|
+
* thresholds that have been crossed as a result of this spend.
|
|
63
|
+
*
|
|
64
|
+
* @param entry - Cost record with agent, model, token counts, and optional cost.
|
|
65
|
+
* @returns The persisted TokenUsage record with calculated cost_usd.
|
|
66
|
+
*/
|
|
67
|
+
trackCost(entry: CostEntry): Promise<ApiResult<TokenUsage>>;
|
|
68
|
+
/**
|
|
69
|
+
* Predict the cost of an LLM call before it is executed.
|
|
70
|
+
*
|
|
71
|
+
* Uses the pricing tables and token estimation heuristics to return
|
|
72
|
+
* an upper-bound cost estimate with a confidence score.
|
|
73
|
+
*
|
|
74
|
+
* @param request - Prediction request with model, prompt text, and token cap.
|
|
75
|
+
* @returns WorkloadForecast with estimated tokens, cost, and confidence.
|
|
76
|
+
*/
|
|
77
|
+
predictCost(request: PredictCostRequest): Promise<ApiResult<WorkloadForecast>>;
|
|
78
|
+
/**
|
|
79
|
+
* Update the budget limits for an agent.
|
|
80
|
+
*
|
|
81
|
+
* Supports updating daily, weekly, and monthly limits independently.
|
|
82
|
+
* Only fields provided in the request are modified; omitted fields retain
|
|
83
|
+
* their current values.
|
|
84
|
+
*
|
|
85
|
+
* @param request - Budget limit update payload.
|
|
86
|
+
* @returns The updated Budget configuration record.
|
|
87
|
+
*/
|
|
88
|
+
setBudgetLimit(request: SetBudgetLimitRequest): Promise<ApiResult<Budget>>;
|
|
89
|
+
/**
|
|
90
|
+
* Retrieve a consolidated usage report for an agent across all periods.
|
|
91
|
+
*
|
|
92
|
+
* Returns daily, weekly, and monthly BudgetStatus snapshots plus a
|
|
93
|
+
* lifetime total spend.
|
|
94
|
+
*
|
|
95
|
+
* @param agentId - The unique agent identifier.
|
|
96
|
+
* @param options - Optional filter by period ("daily" | "weekly" | "monthly").
|
|
97
|
+
* @returns UsageReport with per-period snapshots and lifetime totals.
|
|
98
|
+
*/
|
|
99
|
+
getUsageReport(agentId: string, options?: {
|
|
100
|
+
period?: "daily" | "weekly" | "monthly";
|
|
101
|
+
}): Promise<ApiResult<UsageReport>>;
|
|
102
|
+
/**
|
|
103
|
+
* Retrieve pricing information for a specific model.
|
|
104
|
+
*
|
|
105
|
+
* Supports fuzzy model name resolution including aliases (e.g. "haiku"
|
|
106
|
+
* resolves to "claude-haiku-4"). Returns null in the data field if the
|
|
107
|
+
* model cannot be resolved.
|
|
108
|
+
*
|
|
109
|
+
* @param model - Model identifier string (exact or alias).
|
|
110
|
+
* @returns ModelPricing with per-million token rates, tier, and context window.
|
|
111
|
+
*/
|
|
112
|
+
getModelPricing(model: string): Promise<ApiResult<ModelPricing>>;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create a typed HTTP client for the agent-energy-budget service.
|
|
116
|
+
*
|
|
117
|
+
* @param config - Client configuration including base URL.
|
|
118
|
+
* @returns An AgentEnergyBudgetClient instance.
|
|
119
|
+
*/
|
|
120
|
+
export declare function createAgentEnergyBudgetClient(config: AgentEnergyBudgetClientConfig): AgentEnergyBudgetClient;
|
|
121
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,KAAK,EAEV,SAAS,EACT,MAAM,EACN,SAAS,EACT,YAAY,EACZ,kBAAkB,EAClB,qBAAqB,EACrB,UAAU,EACV,WAAW,EACX,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAMpB,6DAA6D;AAC7D,MAAM,WAAW,6BAA6B;IAC5C,iFAAiF;IACjF,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,iEAAiE;IACjE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,2DAA2D;IAC3D,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACrD;AA0DD,6DAA6D;AAC7D,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;OAQG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;IAEvD;;;;;;;;OAQG;IACH,SAAS,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;IAE5D;;;;;;;;OAQG;IACH,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC,CAAC;IAE/E;;;;;;;;;OASG;IACH,cAAc,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;IAE3E;;;;;;;;;OASG;IACH,cAAc,CACZ,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAA;KAAE,GACpD,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC;IAEnC;;;;;;;;;OASG;IACH,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAAC;CAClE;AAMD;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,6BAA6B,GACpC,uBAAuB,CAgFzB"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client for the agent-energy-budget service API.
|
|
3
|
+
*
|
|
4
|
+
* Uses the Fetch API (available natively in Node 18+, browsers, and Deno).
|
|
5
|
+
* No external dependencies required.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { createAgentEnergyBudgetClient } from "@aumos/agent-energy-budget";
|
|
10
|
+
*
|
|
11
|
+
* const client = createAgentEnergyBudgetClient({ baseUrl: "http://localhost:8095" });
|
|
12
|
+
*
|
|
13
|
+
* // Check current budget
|
|
14
|
+
* const budget = await client.getBudget("my-agent");
|
|
15
|
+
* if (budget.ok) {
|
|
16
|
+
* console.log("Daily limit:", budget.data.daily_limit, "USD");
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Record a completed LLM call
|
|
20
|
+
* await client.trackCost({
|
|
21
|
+
* agent_id: "my-agent",
|
|
22
|
+
* model: "claude-haiku-4",
|
|
23
|
+
* input_tokens: 2048,
|
|
24
|
+
* output_tokens: 512,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Predict cost before making an LLM call
|
|
28
|
+
* const forecast = await client.predictCost({
|
|
29
|
+
* agent_id: "my-agent",
|
|
30
|
+
* model: "gpt-4o-mini",
|
|
31
|
+
* prompt_text: "Summarize the report.",
|
|
32
|
+
* max_output_tokens: 256,
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Internal helpers
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
async function fetchJson(url, init, timeoutMs) {
|
|
40
|
+
const controller = new AbortController();
|
|
41
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetch(url, { ...init, signal: controller.signal });
|
|
44
|
+
clearTimeout(timeoutId);
|
|
45
|
+
const body = await response.json();
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
const errorBody = body;
|
|
48
|
+
return {
|
|
49
|
+
ok: false,
|
|
50
|
+
error: {
|
|
51
|
+
error: errorBody.error ?? "Unknown error",
|
|
52
|
+
detail: errorBody.detail ?? "",
|
|
53
|
+
},
|
|
54
|
+
status: response.status,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return { ok: true, data: body };
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
clearTimeout(timeoutId);
|
|
61
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
62
|
+
return {
|
|
63
|
+
ok: false,
|
|
64
|
+
error: { error: "Network error", detail: message },
|
|
65
|
+
status: 0,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function buildHeaders(extraHeaders) {
|
|
70
|
+
return {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
Accept: "application/json",
|
|
73
|
+
...extraHeaders,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// Client factory
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
/**
|
|
80
|
+
* Create a typed HTTP client for the agent-energy-budget service.
|
|
81
|
+
*
|
|
82
|
+
* @param config - Client configuration including base URL.
|
|
83
|
+
* @returns An AgentEnergyBudgetClient instance.
|
|
84
|
+
*/
|
|
85
|
+
export function createAgentEnergyBudgetClient(config) {
|
|
86
|
+
const { baseUrl, timeoutMs = 30000, headers: extraHeaders } = config;
|
|
87
|
+
const baseHeaders = buildHeaders(extraHeaders);
|
|
88
|
+
return {
|
|
89
|
+
async getBudget(agentId) {
|
|
90
|
+
return fetchJson(`${baseUrl}/budgets/${encodeURIComponent(agentId)}`, { method: "GET", headers: baseHeaders }, timeoutMs);
|
|
91
|
+
},
|
|
92
|
+
async trackCost(entry) {
|
|
93
|
+
return fetchJson(`${baseUrl}/costs`, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: baseHeaders,
|
|
96
|
+
body: JSON.stringify(entry),
|
|
97
|
+
}, timeoutMs);
|
|
98
|
+
},
|
|
99
|
+
async predictCost(request) {
|
|
100
|
+
return fetchJson(`${baseUrl}/costs/predict`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: baseHeaders,
|
|
103
|
+
body: JSON.stringify(request),
|
|
104
|
+
}, timeoutMs);
|
|
105
|
+
},
|
|
106
|
+
async setBudgetLimit(request) {
|
|
107
|
+
return fetchJson(`${baseUrl}/budgets/${encodeURIComponent(request.agent_id)}/limits`, {
|
|
108
|
+
method: "PATCH",
|
|
109
|
+
headers: baseHeaders,
|
|
110
|
+
body: JSON.stringify(request),
|
|
111
|
+
}, timeoutMs);
|
|
112
|
+
},
|
|
113
|
+
async getUsageReport(agentId, options) {
|
|
114
|
+
const params = new URLSearchParams();
|
|
115
|
+
if (options?.period !== undefined) {
|
|
116
|
+
params.set("period", options.period);
|
|
117
|
+
}
|
|
118
|
+
const queryString = params.toString();
|
|
119
|
+
const url = queryString
|
|
120
|
+
? `${baseUrl}/reports/${encodeURIComponent(agentId)}?${queryString}`
|
|
121
|
+
: `${baseUrl}/reports/${encodeURIComponent(agentId)}`;
|
|
122
|
+
return fetchJson(url, { method: "GET", headers: baseHeaders }, timeoutMs);
|
|
123
|
+
},
|
|
124
|
+
async getModelPricing(model) {
|
|
125
|
+
return fetchJson(`${baseUrl}/pricing/${encodeURIComponent(model)}`, { method: "GET", headers: baseHeaders }, timeoutMs);
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AA6BH,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E;AAE9E,KAAK,UAAU,SAAS,CACtB,GAAW,EACX,IAAiB,EACjB,SAAiB;IAEjB,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,SAAS,CAAC,CAAC;IAElE,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;QAC1E,YAAY,CAAC,SAAS,CAAC,CAAC;QAExB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAa,CAAC;QAE9C,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,SAAS,GAAG,IAAyB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AC,OAAO,CAAC,EAAE,CAAC;YACxD,OAAO,SAAS,CACd,GAAG,EACH,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,EACvC,SAAS,CACV,CAAC;QACJ,CAAC;QAED,KAAK,CAAC,eAAe,CAAC,KAAa;YACjC,OAAO,SAAS,CACd,GAAG,OAAO,YAAY,kBAAkB,CAAC,KAAK,CAAC,EAAE,EACjD,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,EACvC,SAAS,CACV,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @aumos/agent-energy-budget
|
|
3
|
+
*
|
|
4
|
+
* TypeScript client for the AumOS agent-energy-budget service.
|
|
5
|
+
* Provides HTTP client and type definitions for token cost tracking,
|
|
6
|
+
* budget enforcement, cost prediction, and model pricing lookup.
|
|
7
|
+
*/
|
|
8
|
+
export type { AgentEnergyBudgetClient, AgentEnergyBudgetClientConfig } from "./client.js";
|
|
9
|
+
export { createAgentEnergyBudgetClient } from "./client.js";
|
|
10
|
+
export type { DegradationStrategy, ProviderName, ModelTier, AlertLevel, TokenUsage, AlertThresholds, ModelPreferences, Budget, BudgetStatus, CostEntry, ModelPricing, BudgetAlert, WorkloadForecast, PredictCostRequest, SetBudgetLimitRequest, ThrottleConfig, UsageReport, ApiError, ApiResult, } from "./types.js";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,YAAY,EAAE,uBAAuB,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC;AAC1F,OAAO,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC;AAG5D,YAAY,EACV,mBAAmB,EACnB,YAAY,EACZ,SAAS,EACT,UAAU,EACV,UAAU,EACV,eAAe,EACf,gBAAgB,EAChB,MAAM,EACN,YAAY,EACZ,SAAS,EACT,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,EACrB,cAAc,EACd,WAAW,EACX,QAAQ,EACR,SAAS,GACV,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @aumos/agent-energy-budget
|
|
3
|
+
*
|
|
4
|
+
* TypeScript client for the AumOS agent-energy-budget service.
|
|
5
|
+
* Provides HTTP client and type definitions for token cost tracking,
|
|
6
|
+
* budget enforcement, cost prediction, and model pricing lookup.
|
|
7
|
+
*/
|
|
8
|
+
export { createAgentEnergyBudgetClient } from "./client.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript interfaces for the agent-energy-budget service.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the Pydantic models and dataclasses defined in:
|
|
5
|
+
* agent_energy_budget.budget.config
|
|
6
|
+
* agent_energy_budget.budget.tracker
|
|
7
|
+
* agent_energy_budget.budget.alerts
|
|
8
|
+
* agent_energy_budget.pricing.tables
|
|
9
|
+
* agent_energy_budget.estimator.cost_estimator
|
|
10
|
+
*
|
|
11
|
+
* All interfaces use readonly fields to match Python's frozen dataclasses.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Action to take when a budget limit is approached or exceeded.
|
|
15
|
+
* Maps to DegradationStrategy enum in agent_energy_budget.budget.config.
|
|
16
|
+
*/
|
|
17
|
+
export type DegradationStrategy = "model_downgrade" | "token_reduction" | "block_with_error" | "cached_fallback";
|
|
18
|
+
/**
|
|
19
|
+
* LLM provider identifier.
|
|
20
|
+
* Maps to ProviderName enum in agent_energy_budget.pricing.tables.
|
|
21
|
+
*/
|
|
22
|
+
export type ProviderName = "anthropic" | "openai" | "google" | "mistral" | "meta" | "deepseek" | "custom";
|
|
23
|
+
/**
|
|
24
|
+
* Quality/cost tier classification for models.
|
|
25
|
+
* Maps to ModelTier enum in agent_energy_budget.pricing.tables.
|
|
26
|
+
*
|
|
27
|
+
* nano — ultra-cheap, small context, fast
|
|
28
|
+
* efficient — good value, solid quality
|
|
29
|
+
* standard — mainstream flagship-class models
|
|
30
|
+
* premium — top capability, highest cost
|
|
31
|
+
*/
|
|
32
|
+
export type ModelTier = "nano" | "efficient" | "standard" | "premium";
|
|
33
|
+
/**
|
|
34
|
+
* Alert severity level.
|
|
35
|
+
* Maps to AlertLevel enum in agent_energy_budget.budget.alerts.
|
|
36
|
+
*/
|
|
37
|
+
export type AlertLevel = "warning" | "critical" | "exhausted";
|
|
38
|
+
/**
|
|
39
|
+
* Token consumption record for a single LLM call.
|
|
40
|
+
* Mirrors the _CallRecord internal dataclass in agent_energy_budget.budget.tracker.
|
|
41
|
+
*/
|
|
42
|
+
export interface TokenUsage {
|
|
43
|
+
/** Agent that made this call. */
|
|
44
|
+
readonly agent_id: string;
|
|
45
|
+
/** Model identifier used for this call. */
|
|
46
|
+
readonly model: string;
|
|
47
|
+
/** Number of input (prompt) tokens consumed. */
|
|
48
|
+
readonly input_tokens: number;
|
|
49
|
+
/** Number of output (completion) tokens generated. */
|
|
50
|
+
readonly output_tokens: number;
|
|
51
|
+
/** Actual cost in USD for this call. */
|
|
52
|
+
readonly cost_usd: number;
|
|
53
|
+
/** ISO-8601 UTC timestamp when this call was recorded. */
|
|
54
|
+
readonly recorded_at: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Alert threshold configuration as utilisation percentages.
|
|
58
|
+
* Maps to AlertThresholds in agent_energy_budget.budget.config.
|
|
59
|
+
*/
|
|
60
|
+
export interface AlertThresholds {
|
|
61
|
+
/** First alert threshold (default 50%). */
|
|
62
|
+
readonly warning: number;
|
|
63
|
+
/** Second alert threshold (default 80%). */
|
|
64
|
+
readonly critical: number;
|
|
65
|
+
/** Third alert threshold (default 100%). */
|
|
66
|
+
readonly exhausted: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Model selection preferences for degradation and estimation.
|
|
70
|
+
* Maps to ModelPreferences in agent_energy_budget.budget.config.
|
|
71
|
+
*/
|
|
72
|
+
export interface ModelPreferences {
|
|
73
|
+
/** Ordered list of model IDs to try first (most preferred first). */
|
|
74
|
+
readonly preferred_models: readonly string[];
|
|
75
|
+
/** Model to use when all others are exhausted or over budget. */
|
|
76
|
+
readonly fallback_model: string;
|
|
77
|
+
/** Model IDs that must never be used. */
|
|
78
|
+
readonly blocked_models: readonly string[];
|
|
79
|
+
/** When true, restrict model selection to vision-capable models only. */
|
|
80
|
+
readonly require_vision: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Top-level budget configuration for a single agent or agent group.
|
|
84
|
+
* Maps to BudgetConfig in agent_energy_budget.budget.config.
|
|
85
|
+
*/
|
|
86
|
+
export interface Budget {
|
|
87
|
+
/** Unique identifier for the agent this budget belongs to. */
|
|
88
|
+
readonly agent_id: string;
|
|
89
|
+
/** Maximum USD spend per calendar day (0 = disabled). */
|
|
90
|
+
readonly daily_limit: number;
|
|
91
|
+
/** Maximum USD spend per calendar week Mon-Sun (0 = disabled). */
|
|
92
|
+
readonly weekly_limit: number;
|
|
93
|
+
/** Maximum USD spend per calendar month (0 = disabled). */
|
|
94
|
+
readonly monthly_limit: number;
|
|
95
|
+
/** Strategy to apply when a limit is breached. */
|
|
96
|
+
readonly degradation_strategy: DegradationStrategy;
|
|
97
|
+
/** Utilisation percentage levels that trigger alerts. */
|
|
98
|
+
readonly alert_thresholds: AlertThresholds;
|
|
99
|
+
/** Model selection configuration for degradation. */
|
|
100
|
+
readonly model_preferences: ModelPreferences;
|
|
101
|
+
/** ISO currency code for display purposes (default "USD"). */
|
|
102
|
+
readonly currency: string;
|
|
103
|
+
/** Arbitrary key-value metadata attached to this budget. */
|
|
104
|
+
readonly tags: Readonly<Record<string, string>>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Snapshot of current budget usage for a single period.
|
|
108
|
+
* Maps to BudgetStatus dataclass in agent_energy_budget.budget.tracker.
|
|
109
|
+
*/
|
|
110
|
+
export interface BudgetStatus {
|
|
111
|
+
/** The agent whose budget this describes. */
|
|
112
|
+
readonly agent_id: string;
|
|
113
|
+
/** Period label: "daily", "weekly", or "monthly". */
|
|
114
|
+
readonly period: "daily" | "weekly" | "monthly";
|
|
115
|
+
/** Budget cap in USD for this period. */
|
|
116
|
+
readonly limit_usd: number;
|
|
117
|
+
/** Amount spent so far this period in USD. */
|
|
118
|
+
readonly spent_usd: number;
|
|
119
|
+
/** Amount remaining (limit - spent); may be negative if overspent. */
|
|
120
|
+
readonly remaining_usd: number;
|
|
121
|
+
/** Percentage of budget consumed (0-100+). */
|
|
122
|
+
readonly utilisation_pct: number;
|
|
123
|
+
/** Number of LLM calls recorded this period. */
|
|
124
|
+
readonly call_count: number;
|
|
125
|
+
/** Average cost per call in USD. */
|
|
126
|
+
readonly avg_cost_per_call: number;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Request payload for recording actual LLM call costs.
|
|
130
|
+
*/
|
|
131
|
+
export interface CostEntry {
|
|
132
|
+
/** Agent that made the call. */
|
|
133
|
+
readonly agent_id: string;
|
|
134
|
+
/** Model identifier used. */
|
|
135
|
+
readonly model: string;
|
|
136
|
+
/** Actual input tokens consumed. */
|
|
137
|
+
readonly input_tokens: number;
|
|
138
|
+
/** Actual output tokens generated. */
|
|
139
|
+
readonly output_tokens: number;
|
|
140
|
+
/** Actual cost in USD (if null, calculated from pricing tables). */
|
|
141
|
+
readonly cost_usd?: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Per-model pricing in USD per million tokens.
|
|
145
|
+
* Maps to ModelPricing dataclass in agent_energy_budget.pricing.tables.
|
|
146
|
+
*/
|
|
147
|
+
export interface ModelPricing {
|
|
148
|
+
/** Canonical model identifier string. */
|
|
149
|
+
readonly model: string;
|
|
150
|
+
/** The provider that operates this model. */
|
|
151
|
+
readonly provider: ProviderName;
|
|
152
|
+
/** Quality/cost tier classification. */
|
|
153
|
+
readonly tier: ModelTier;
|
|
154
|
+
/** Cost in USD for one million input (prompt) tokens. */
|
|
155
|
+
readonly input_per_million: number;
|
|
156
|
+
/** Cost in USD for one million output (completion) tokens. */
|
|
157
|
+
readonly output_per_million: number;
|
|
158
|
+
/** Maximum context window in tokens (0 = unknown). */
|
|
159
|
+
readonly context_window: number;
|
|
160
|
+
/** Whether the model accepts image inputs. */
|
|
161
|
+
readonly supports_vision: boolean;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Immutable record of a fired budget alert event.
|
|
165
|
+
* Maps to AlertEvent dataclass in agent_energy_budget.budget.alerts.
|
|
166
|
+
*/
|
|
167
|
+
export interface BudgetAlert {
|
|
168
|
+
/** The agent whose budget triggered the alert. */
|
|
169
|
+
readonly agent_id: string;
|
|
170
|
+
/** Alert severity level. */
|
|
171
|
+
readonly level: AlertLevel;
|
|
172
|
+
/** Current utilisation percentage at the time of the alert. */
|
|
173
|
+
readonly utilisation_pct: number;
|
|
174
|
+
/** Amount spent in USD at the time of the alert. */
|
|
175
|
+
readonly spent_usd: number;
|
|
176
|
+
/** Budget limit in USD that was approached or exceeded. */
|
|
177
|
+
readonly limit_usd: number;
|
|
178
|
+
/** Budget period label ("daily", "weekly", or "monthly"). */
|
|
179
|
+
readonly period: string;
|
|
180
|
+
/** Human-readable description of the alert condition. */
|
|
181
|
+
readonly message: string;
|
|
182
|
+
/** ISO-8601 UTC timestamp when the alert was fired. */
|
|
183
|
+
readonly fired_at: string;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Pre-execution cost estimate for an LLM call.
|
|
187
|
+
* Maps to CostEstimate dataclass in agent_energy_budget.estimator.cost_estimator.
|
|
188
|
+
*/
|
|
189
|
+
export interface WorkloadForecast {
|
|
190
|
+
/** Model identifier used for the estimate. */
|
|
191
|
+
readonly model: string;
|
|
192
|
+
/** Estimated number of input (prompt) tokens. */
|
|
193
|
+
readonly estimated_input_tokens: number;
|
|
194
|
+
/** Estimated number of output (completion) tokens. */
|
|
195
|
+
readonly estimated_output_tokens: number;
|
|
196
|
+
/** Estimated total cost in USD. */
|
|
197
|
+
readonly estimated_cost_usd: number;
|
|
198
|
+
/** Confidence score for the estimate in [0.0, 1.0]. */
|
|
199
|
+
readonly confidence: number;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Request payload for predicting the cost of an LLM call before execution.
|
|
203
|
+
*/
|
|
204
|
+
export interface PredictCostRequest {
|
|
205
|
+
/** Agent identifier requesting the cost prediction. */
|
|
206
|
+
readonly agent_id: string;
|
|
207
|
+
/** Model to use for cost estimation. */
|
|
208
|
+
readonly model: string;
|
|
209
|
+
/** Prompt text to estimate input tokens from. */
|
|
210
|
+
readonly prompt_text: string;
|
|
211
|
+
/** Maximum output tokens (used as worst-case upper bound). */
|
|
212
|
+
readonly max_output_tokens?: number;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Request payload for updating budget limits for an agent.
|
|
216
|
+
*/
|
|
217
|
+
export interface SetBudgetLimitRequest {
|
|
218
|
+
/** Agent identifier to update limits for. */
|
|
219
|
+
readonly agent_id: string;
|
|
220
|
+
/** New daily limit in USD (0 = disabled, omit to leave unchanged). */
|
|
221
|
+
readonly daily_limit?: number;
|
|
222
|
+
/** New weekly limit in USD (0 = disabled, omit to leave unchanged). */
|
|
223
|
+
readonly weekly_limit?: number;
|
|
224
|
+
/** New monthly limit in USD (0 = disabled, omit to leave unchanged). */
|
|
225
|
+
readonly monthly_limit?: number;
|
|
226
|
+
/** Degradation strategy to apply on limit breach. */
|
|
227
|
+
readonly degradation_strategy?: DegradationStrategy;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Throttling configuration applied when budget limits approach exhaustion.
|
|
231
|
+
* Maps to options in agent_energy_budget.budget.config.
|
|
232
|
+
*/
|
|
233
|
+
export interface ThrottleConfig {
|
|
234
|
+
/** Agent identifier this throttle configuration applies to. */
|
|
235
|
+
readonly agent_id: string;
|
|
236
|
+
/** Whether throttling is currently active for this agent. */
|
|
237
|
+
readonly throttle_active: boolean;
|
|
238
|
+
/** Maximum calls per minute allowed under throttle. */
|
|
239
|
+
readonly max_calls_per_minute: number;
|
|
240
|
+
/** Maximum input tokens per call under throttle. */
|
|
241
|
+
readonly max_input_tokens_per_call: number;
|
|
242
|
+
/** Maximum output tokens per call under throttle. */
|
|
243
|
+
readonly max_output_tokens_per_call: number;
|
|
244
|
+
/** ISO-8601 UTC timestamp when throttling was last applied. */
|
|
245
|
+
readonly throttle_applied_at: string | null;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Aggregated usage report across all periods for an agent.
|
|
249
|
+
*/
|
|
250
|
+
export interface UsageReport {
|
|
251
|
+
/** Agent identifier this report covers. */
|
|
252
|
+
readonly agent_id: string;
|
|
253
|
+
/** Daily usage snapshot. */
|
|
254
|
+
readonly daily: BudgetStatus;
|
|
255
|
+
/** Weekly usage snapshot. */
|
|
256
|
+
readonly weekly: BudgetStatus;
|
|
257
|
+
/** Monthly usage snapshot. */
|
|
258
|
+
readonly monthly: BudgetStatus;
|
|
259
|
+
/** Total USD spent across all recorded history. */
|
|
260
|
+
readonly lifetime_spend_usd: number;
|
|
261
|
+
/** ISO-8601 UTC timestamp when this report was generated. */
|
|
262
|
+
readonly generated_at: string;
|
|
263
|
+
}
|
|
264
|
+
/** Standard error payload returned by the agent-energy-budget API. */
|
|
265
|
+
export interface ApiError {
|
|
266
|
+
readonly error: string;
|
|
267
|
+
readonly detail: string;
|
|
268
|
+
}
|
|
269
|
+
/** Result type for all client operations. */
|
|
270
|
+
export type ApiResult<T> = {
|
|
271
|
+
readonly ok: true;
|
|
272
|
+
readonly data: T;
|
|
273
|
+
} | {
|
|
274
|
+
readonly ok: false;
|
|
275
|
+
readonly error: ApiError;
|
|
276
|
+
readonly status: number;
|
|
277
|
+
};
|
|
278
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAMH;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAC3B,iBAAiB,GACjB,iBAAiB,GACjB,kBAAkB,GAClB,iBAAiB,CAAC;AAEtB;;;GAGG;AACH,MAAM,MAAM,YAAY,GACpB,WAAW,GACX,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,MAAM,GACN,UAAU,GACV,QAAQ,CAAC;AAEb;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,GAAG,SAAS,CAAC;AAEtE;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,UAAU,GAAG,WAAW,CAAC;AAM9D;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,iCAAiC;IACjC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,2CAA2C;IAC3C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,gDAAgD;IAChD,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,sDAAsD;IACtD,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,wCAAwC;IACxC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,0DAA0D;IAC1D,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B;AAMD;;;GAGG;AACH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AAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,6CAA6C;AAC7C,MAAM,MAAM,SAAS,CAAC,CAAC,IACnB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAA;CAAE,GACvC;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript interfaces for the agent-energy-budget service.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the Pydantic models and dataclasses defined in:
|
|
5
|
+
* agent_energy_budget.budget.config
|
|
6
|
+
* agent_energy_budget.budget.tracker
|
|
7
|
+
* agent_energy_budget.budget.alerts
|
|
8
|
+
* agent_energy_budget.pricing.tables
|
|
9
|
+
* agent_energy_budget.estimator.cost_estimator
|
|
10
|
+
*
|
|
11
|
+
* All interfaces use readonly fields to match Python's frozen dataclasses.
|
|
12
|
+
*/
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aumos/agent-energy-budget",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript client for the AumOS agent-energy-budget service — token cost tracking, budget enforcement, cost prediction, and model pricing",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"typecheck": "tsc --noEmit"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"typescript": "^5.3.0"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"aumos",
|
|
24
|
+
"agent-energy-budget",
|
|
25
|
+
"budget",
|
|
26
|
+
"cost-tracking",
|
|
27
|
+
"token-usage",
|
|
28
|
+
"model-pricing",
|
|
29
|
+
"llm-costs",
|
|
30
|
+
"typescript"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/aumos-ai/agent-energy-budget"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client for the agent-energy-budget service API.
|
|
3
|
+
*
|
|
4
|
+
* Uses the Fetch API (available natively in Node 18+, browsers, and Deno).
|
|
5
|
+
* No external dependencies required.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { createAgentEnergyBudgetClient } from "@aumos/agent-energy-budget";
|
|
10
|
+
*
|
|
11
|
+
* const client = createAgentEnergyBudgetClient({ baseUrl: "http://localhost:8095" });
|
|
12
|
+
*
|
|
13
|
+
* // Check current budget
|
|
14
|
+
* const budget = await client.getBudget("my-agent");
|
|
15
|
+
* if (budget.ok) {
|
|
16
|
+
* console.log("Daily limit:", budget.data.daily_limit, "USD");
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Record a completed LLM call
|
|
20
|
+
* await client.trackCost({
|
|
21
|
+
* agent_id: "my-agent",
|
|
22
|
+
* model: "claude-haiku-4",
|
|
23
|
+
* input_tokens: 2048,
|
|
24
|
+
* output_tokens: 512,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Predict cost before making an LLM call
|
|
28
|
+
* const forecast = await client.predictCost({
|
|
29
|
+
* agent_id: "my-agent",
|
|
30
|
+
* model: "gpt-4o-mini",
|
|
31
|
+
* prompt_text: "Summarize the report.",
|
|
32
|
+
* max_output_tokens: 256,
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import type {
|
|
38
|
+
ApiError,
|
|
39
|
+
ApiResult,
|
|
40
|
+
Budget,
|
|
41
|
+
CostEntry,
|
|
42
|
+
ModelPricing,
|
|
43
|
+
PredictCostRequest,
|
|
44
|
+
SetBudgetLimitRequest,
|
|
45
|
+
TokenUsage,
|
|
46
|
+
UsageReport,
|
|
47
|
+
WorkloadForecast,
|
|
48
|
+
} from "./types.js";
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Client configuration
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
/** Configuration options for the AgentEnergyBudgetClient. */
|
|
55
|
+
export interface AgentEnergyBudgetClientConfig {
|
|
56
|
+
/** Base URL of the agent-energy-budget server (e.g. "http://localhost:8095"). */
|
|
57
|
+
readonly baseUrl: string;
|
|
58
|
+
/** Optional request timeout in milliseconds (default: 30000). */
|
|
59
|
+
readonly timeoutMs?: number;
|
|
60
|
+
/** Optional extra HTTP headers sent with every request. */
|
|
61
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
// Internal helpers
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
async function fetchJson<T>(
|
|
69
|
+
url: string,
|
|
70
|
+
init: RequestInit,
|
|
71
|
+
timeoutMs: number,
|
|
72
|
+
): Promise<ApiResult<T>> {
|
|
73
|
+
const controller = new AbortController();
|
|
74
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetch(url, { ...init, signal: controller.signal });
|
|
78
|
+
clearTimeout(timeoutId);
|
|
79
|
+
|
|
80
|
+
const body = await response.json() as unknown;
|
|
81
|
+
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
const errorBody = body as Partial<ApiError>;
|
|
84
|
+
return {
|
|
85
|
+
ok: false,
|
|
86
|
+
error: {
|
|
87
|
+
error: errorBody.error ?? "Unknown error",
|
|
88
|
+
detail: errorBody.detail ?? "",
|
|
89
|
+
},
|
|
90
|
+
status: response.status,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return { ok: true, data: body as T };
|
|
95
|
+
} catch (err: unknown) {
|
|
96
|
+
clearTimeout(timeoutId);
|
|
97
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
error: { error: "Network error", detail: message },
|
|
101
|
+
status: 0,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function buildHeaders(
|
|
107
|
+
extraHeaders: Readonly<Record<string, string>> | undefined,
|
|
108
|
+
): Record<string, string> {
|
|
109
|
+
return {
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
Accept: "application/json",
|
|
112
|
+
...extraHeaders,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
// Client interface
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
/** Typed HTTP client for the agent-energy-budget service. */
|
|
121
|
+
export interface AgentEnergyBudgetClient {
|
|
122
|
+
/**
|
|
123
|
+
* Retrieve the current budget configuration for an agent.
|
|
124
|
+
*
|
|
125
|
+
* Returns daily, weekly, and monthly limits alongside the configured
|
|
126
|
+
* degradation strategy, alert thresholds, and model preferences.
|
|
127
|
+
*
|
|
128
|
+
* @param agentId - The unique agent identifier.
|
|
129
|
+
* @returns The Budget configuration record for this agent.
|
|
130
|
+
*/
|
|
131
|
+
getBudget(agentId: string): Promise<ApiResult<Budget>>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Record the actual cost of a completed LLM call.
|
|
135
|
+
*
|
|
136
|
+
* Persists the call record to JSONL storage and fires any alert
|
|
137
|
+
* thresholds that have been crossed as a result of this spend.
|
|
138
|
+
*
|
|
139
|
+
* @param entry - Cost record with agent, model, token counts, and optional cost.
|
|
140
|
+
* @returns The persisted TokenUsage record with calculated cost_usd.
|
|
141
|
+
*/
|
|
142
|
+
trackCost(entry: CostEntry): Promise<ApiResult<TokenUsage>>;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Predict the cost of an LLM call before it is executed.
|
|
146
|
+
*
|
|
147
|
+
* Uses the pricing tables and token estimation heuristics to return
|
|
148
|
+
* an upper-bound cost estimate with a confidence score.
|
|
149
|
+
*
|
|
150
|
+
* @param request - Prediction request with model, prompt text, and token cap.
|
|
151
|
+
* @returns WorkloadForecast with estimated tokens, cost, and confidence.
|
|
152
|
+
*/
|
|
153
|
+
predictCost(request: PredictCostRequest): Promise<ApiResult<WorkloadForecast>>;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Update the budget limits for an agent.
|
|
157
|
+
*
|
|
158
|
+
* Supports updating daily, weekly, and monthly limits independently.
|
|
159
|
+
* Only fields provided in the request are modified; omitted fields retain
|
|
160
|
+
* their current values.
|
|
161
|
+
*
|
|
162
|
+
* @param request - Budget limit update payload.
|
|
163
|
+
* @returns The updated Budget configuration record.
|
|
164
|
+
*/
|
|
165
|
+
setBudgetLimit(request: SetBudgetLimitRequest): Promise<ApiResult<Budget>>;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Retrieve a consolidated usage report for an agent across all periods.
|
|
169
|
+
*
|
|
170
|
+
* Returns daily, weekly, and monthly BudgetStatus snapshots plus a
|
|
171
|
+
* lifetime total spend.
|
|
172
|
+
*
|
|
173
|
+
* @param agentId - The unique agent identifier.
|
|
174
|
+
* @param options - Optional filter by period ("daily" | "weekly" | "monthly").
|
|
175
|
+
* @returns UsageReport with per-period snapshots and lifetime totals.
|
|
176
|
+
*/
|
|
177
|
+
getUsageReport(
|
|
178
|
+
agentId: string,
|
|
179
|
+
options?: { period?: "daily" | "weekly" | "monthly" },
|
|
180
|
+
): Promise<ApiResult<UsageReport>>;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Retrieve pricing information for a specific model.
|
|
184
|
+
*
|
|
185
|
+
* Supports fuzzy model name resolution including aliases (e.g. "haiku"
|
|
186
|
+
* resolves to "claude-haiku-4"). Returns null in the data field if the
|
|
187
|
+
* model cannot be resolved.
|
|
188
|
+
*
|
|
189
|
+
* @param model - Model identifier string (exact or alias).
|
|
190
|
+
* @returns ModelPricing with per-million token rates, tier, and context window.
|
|
191
|
+
*/
|
|
192
|
+
getModelPricing(model: string): Promise<ApiResult<ModelPricing>>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ---------------------------------------------------------------------------
|
|
196
|
+
// Client factory
|
|
197
|
+
// ---------------------------------------------------------------------------
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Create a typed HTTP client for the agent-energy-budget service.
|
|
201
|
+
*
|
|
202
|
+
* @param config - Client configuration including base URL.
|
|
203
|
+
* @returns An AgentEnergyBudgetClient instance.
|
|
204
|
+
*/
|
|
205
|
+
export function createAgentEnergyBudgetClient(
|
|
206
|
+
config: AgentEnergyBudgetClientConfig,
|
|
207
|
+
): AgentEnergyBudgetClient {
|
|
208
|
+
const { baseUrl, timeoutMs = 30_000, headers: extraHeaders } = config;
|
|
209
|
+
const baseHeaders = buildHeaders(extraHeaders);
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
async getBudget(agentId: string): Promise<ApiResult<Budget>> {
|
|
213
|
+
return fetchJson<Budget>(
|
|
214
|
+
`${baseUrl}/budgets/${encodeURIComponent(agentId)}`,
|
|
215
|
+
{ method: "GET", headers: baseHeaders },
|
|
216
|
+
timeoutMs,
|
|
217
|
+
);
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
async trackCost(entry: CostEntry): Promise<ApiResult<TokenUsage>> {
|
|
221
|
+
return fetchJson<TokenUsage>(
|
|
222
|
+
`${baseUrl}/costs`,
|
|
223
|
+
{
|
|
224
|
+
method: "POST",
|
|
225
|
+
headers: baseHeaders,
|
|
226
|
+
body: JSON.stringify(entry),
|
|
227
|
+
},
|
|
228
|
+
timeoutMs,
|
|
229
|
+
);
|
|
230
|
+
},
|
|
231
|
+
|
|
232
|
+
async predictCost(
|
|
233
|
+
request: PredictCostRequest,
|
|
234
|
+
): Promise<ApiResult<WorkloadForecast>> {
|
|
235
|
+
return fetchJson<WorkloadForecast>(
|
|
236
|
+
`${baseUrl}/costs/predict`,
|
|
237
|
+
{
|
|
238
|
+
method: "POST",
|
|
239
|
+
headers: baseHeaders,
|
|
240
|
+
body: JSON.stringify(request),
|
|
241
|
+
},
|
|
242
|
+
timeoutMs,
|
|
243
|
+
);
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
async setBudgetLimit(
|
|
247
|
+
request: SetBudgetLimitRequest,
|
|
248
|
+
): Promise<ApiResult<Budget>> {
|
|
249
|
+
return fetchJson<Budget>(
|
|
250
|
+
`${baseUrl}/budgets/${encodeURIComponent(request.agent_id)}/limits`,
|
|
251
|
+
{
|
|
252
|
+
method: "PATCH",
|
|
253
|
+
headers: baseHeaders,
|
|
254
|
+
body: JSON.stringify(request),
|
|
255
|
+
},
|
|
256
|
+
timeoutMs,
|
|
257
|
+
);
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
async getUsageReport(
|
|
261
|
+
agentId: string,
|
|
262
|
+
options?: { period?: "daily" | "weekly" | "monthly" },
|
|
263
|
+
): Promise<ApiResult<UsageReport>> {
|
|
264
|
+
const params = new URLSearchParams();
|
|
265
|
+
if (options?.period !== undefined) {
|
|
266
|
+
params.set("period", options.period);
|
|
267
|
+
}
|
|
268
|
+
const queryString = params.toString();
|
|
269
|
+
const url = queryString
|
|
270
|
+
? `${baseUrl}/reports/${encodeURIComponent(agentId)}?${queryString}`
|
|
271
|
+
: `${baseUrl}/reports/${encodeURIComponent(agentId)}`;
|
|
272
|
+
return fetchJson<UsageReport>(
|
|
273
|
+
url,
|
|
274
|
+
{ method: "GET", headers: baseHeaders },
|
|
275
|
+
timeoutMs,
|
|
276
|
+
);
|
|
277
|
+
},
|
|
278
|
+
|
|
279
|
+
async getModelPricing(model: string): Promise<ApiResult<ModelPricing>> {
|
|
280
|
+
return fetchJson<ModelPricing>(
|
|
281
|
+
`${baseUrl}/pricing/${encodeURIComponent(model)}`,
|
|
282
|
+
{ method: "GET", headers: baseHeaders },
|
|
283
|
+
timeoutMs,
|
|
284
|
+
);
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @aumos/agent-energy-budget
|
|
3
|
+
*
|
|
4
|
+
* TypeScript client for the AumOS agent-energy-budget service.
|
|
5
|
+
* Provides HTTP client and type definitions for token cost tracking,
|
|
6
|
+
* budget enforcement, cost prediction, and model pricing lookup.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// Client and configuration
|
|
10
|
+
export type { AgentEnergyBudgetClient, AgentEnergyBudgetClientConfig } from "./client.js";
|
|
11
|
+
export { createAgentEnergyBudgetClient } from "./client.js";
|
|
12
|
+
|
|
13
|
+
// Core types
|
|
14
|
+
export type {
|
|
15
|
+
DegradationStrategy,
|
|
16
|
+
ProviderName,
|
|
17
|
+
ModelTier,
|
|
18
|
+
AlertLevel,
|
|
19
|
+
TokenUsage,
|
|
20
|
+
AlertThresholds,
|
|
21
|
+
ModelPreferences,
|
|
22
|
+
Budget,
|
|
23
|
+
BudgetStatus,
|
|
24
|
+
CostEntry,
|
|
25
|
+
ModelPricing,
|
|
26
|
+
BudgetAlert,
|
|
27
|
+
WorkloadForecast,
|
|
28
|
+
PredictCostRequest,
|
|
29
|
+
SetBudgetLimitRequest,
|
|
30
|
+
ThrottleConfig,
|
|
31
|
+
UsageReport,
|
|
32
|
+
ApiError,
|
|
33
|
+
ApiResult,
|
|
34
|
+
} from "./types.js";
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript interfaces for the agent-energy-budget service.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the Pydantic models and dataclasses defined in:
|
|
5
|
+
* agent_energy_budget.budget.config
|
|
6
|
+
* agent_energy_budget.budget.tracker
|
|
7
|
+
* agent_energy_budget.budget.alerts
|
|
8
|
+
* agent_energy_budget.pricing.tables
|
|
9
|
+
* agent_energy_budget.estimator.cost_estimator
|
|
10
|
+
*
|
|
11
|
+
* All interfaces use readonly fields to match Python's frozen dataclasses.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Enumerations
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Action to take when a budget limit is approached or exceeded.
|
|
20
|
+
* Maps to DegradationStrategy enum in agent_energy_budget.budget.config.
|
|
21
|
+
*/
|
|
22
|
+
export type DegradationStrategy =
|
|
23
|
+
| "model_downgrade"
|
|
24
|
+
| "token_reduction"
|
|
25
|
+
| "block_with_error"
|
|
26
|
+
| "cached_fallback";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* LLM provider identifier.
|
|
30
|
+
* Maps to ProviderName enum in agent_energy_budget.pricing.tables.
|
|
31
|
+
*/
|
|
32
|
+
export type ProviderName =
|
|
33
|
+
| "anthropic"
|
|
34
|
+
| "openai"
|
|
35
|
+
| "google"
|
|
36
|
+
| "mistral"
|
|
37
|
+
| "meta"
|
|
38
|
+
| "deepseek"
|
|
39
|
+
| "custom";
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Quality/cost tier classification for models.
|
|
43
|
+
* Maps to ModelTier enum in agent_energy_budget.pricing.tables.
|
|
44
|
+
*
|
|
45
|
+
* nano — ultra-cheap, small context, fast
|
|
46
|
+
* efficient — good value, solid quality
|
|
47
|
+
* standard — mainstream flagship-class models
|
|
48
|
+
* premium — top capability, highest cost
|
|
49
|
+
*/
|
|
50
|
+
export type ModelTier = "nano" | "efficient" | "standard" | "premium";
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Alert severity level.
|
|
54
|
+
* Maps to AlertLevel enum in agent_energy_budget.budget.alerts.
|
|
55
|
+
*/
|
|
56
|
+
export type AlertLevel = "warning" | "critical" | "exhausted";
|
|
57
|
+
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// TokenUsage
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Token consumption record for a single LLM call.
|
|
64
|
+
* Mirrors the _CallRecord internal dataclass in agent_energy_budget.budget.tracker.
|
|
65
|
+
*/
|
|
66
|
+
export interface TokenUsage {
|
|
67
|
+
/** Agent that made this call. */
|
|
68
|
+
readonly agent_id: string;
|
|
69
|
+
/** Model identifier used for this call. */
|
|
70
|
+
readonly model: string;
|
|
71
|
+
/** Number of input (prompt) tokens consumed. */
|
|
72
|
+
readonly input_tokens: number;
|
|
73
|
+
/** Number of output (completion) tokens generated. */
|
|
74
|
+
readonly output_tokens: number;
|
|
75
|
+
/** Actual cost in USD for this call. */
|
|
76
|
+
readonly cost_usd: number;
|
|
77
|
+
/** ISO-8601 UTC timestamp when this call was recorded. */
|
|
78
|
+
readonly recorded_at: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// Budget
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Alert threshold configuration as utilisation percentages.
|
|
87
|
+
* Maps to AlertThresholds in agent_energy_budget.budget.config.
|
|
88
|
+
*/
|
|
89
|
+
export interface AlertThresholds {
|
|
90
|
+
/** First alert threshold (default 50%). */
|
|
91
|
+
readonly warning: number;
|
|
92
|
+
/** Second alert threshold (default 80%). */
|
|
93
|
+
readonly critical: number;
|
|
94
|
+
/** Third alert threshold (default 100%). */
|
|
95
|
+
readonly exhausted: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Model selection preferences for degradation and estimation.
|
|
100
|
+
* Maps to ModelPreferences in agent_energy_budget.budget.config.
|
|
101
|
+
*/
|
|
102
|
+
export interface ModelPreferences {
|
|
103
|
+
/** Ordered list of model IDs to try first (most preferred first). */
|
|
104
|
+
readonly preferred_models: readonly string[];
|
|
105
|
+
/** Model to use when all others are exhausted or over budget. */
|
|
106
|
+
readonly fallback_model: string;
|
|
107
|
+
/** Model IDs that must never be used. */
|
|
108
|
+
readonly blocked_models: readonly string[];
|
|
109
|
+
/** When true, restrict model selection to vision-capable models only. */
|
|
110
|
+
readonly require_vision: boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Top-level budget configuration for a single agent or agent group.
|
|
115
|
+
* Maps to BudgetConfig in agent_energy_budget.budget.config.
|
|
116
|
+
*/
|
|
117
|
+
export interface Budget {
|
|
118
|
+
/** Unique identifier for the agent this budget belongs to. */
|
|
119
|
+
readonly agent_id: string;
|
|
120
|
+
/** Maximum USD spend per calendar day (0 = disabled). */
|
|
121
|
+
readonly daily_limit: number;
|
|
122
|
+
/** Maximum USD spend per calendar week Mon-Sun (0 = disabled). */
|
|
123
|
+
readonly weekly_limit: number;
|
|
124
|
+
/** Maximum USD spend per calendar month (0 = disabled). */
|
|
125
|
+
readonly monthly_limit: number;
|
|
126
|
+
/** Strategy to apply when a limit is breached. */
|
|
127
|
+
readonly degradation_strategy: DegradationStrategy;
|
|
128
|
+
/** Utilisation percentage levels that trigger alerts. */
|
|
129
|
+
readonly alert_thresholds: AlertThresholds;
|
|
130
|
+
/** Model selection configuration for degradation. */
|
|
131
|
+
readonly model_preferences: ModelPreferences;
|
|
132
|
+
/** ISO currency code for display purposes (default "USD"). */
|
|
133
|
+
readonly currency: string;
|
|
134
|
+
/** Arbitrary key-value metadata attached to this budget. */
|
|
135
|
+
readonly tags: Readonly<Record<string, string>>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
// BudgetStatus (usage snapshot)
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Snapshot of current budget usage for a single period.
|
|
144
|
+
* Maps to BudgetStatus dataclass in agent_energy_budget.budget.tracker.
|
|
145
|
+
*/
|
|
146
|
+
export interface BudgetStatus {
|
|
147
|
+
/** The agent whose budget this describes. */
|
|
148
|
+
readonly agent_id: string;
|
|
149
|
+
/** Period label: "daily", "weekly", or "monthly". */
|
|
150
|
+
readonly period: "daily" | "weekly" | "monthly";
|
|
151
|
+
/** Budget cap in USD for this period. */
|
|
152
|
+
readonly limit_usd: number;
|
|
153
|
+
/** Amount spent so far this period in USD. */
|
|
154
|
+
readonly spent_usd: number;
|
|
155
|
+
/** Amount remaining (limit - spent); may be negative if overspent. */
|
|
156
|
+
readonly remaining_usd: number;
|
|
157
|
+
/** Percentage of budget consumed (0-100+). */
|
|
158
|
+
readonly utilisation_pct: number;
|
|
159
|
+
/** Number of LLM calls recorded this period. */
|
|
160
|
+
readonly call_count: number;
|
|
161
|
+
/** Average cost per call in USD. */
|
|
162
|
+
readonly avg_cost_per_call: number;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
// CostEntry (track cost request)
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Request payload for recording actual LLM call costs.
|
|
171
|
+
*/
|
|
172
|
+
export interface CostEntry {
|
|
173
|
+
/** Agent that made the call. */
|
|
174
|
+
readonly agent_id: string;
|
|
175
|
+
/** Model identifier used. */
|
|
176
|
+
readonly model: string;
|
|
177
|
+
/** Actual input tokens consumed. */
|
|
178
|
+
readonly input_tokens: number;
|
|
179
|
+
/** Actual output tokens generated. */
|
|
180
|
+
readonly output_tokens: number;
|
|
181
|
+
/** Actual cost in USD (if null, calculated from pricing tables). */
|
|
182
|
+
readonly cost_usd?: number;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
// ModelPricing
|
|
187
|
+
// ---------------------------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Per-model pricing in USD per million tokens.
|
|
191
|
+
* Maps to ModelPricing dataclass in agent_energy_budget.pricing.tables.
|
|
192
|
+
*/
|
|
193
|
+
export interface ModelPricing {
|
|
194
|
+
/** Canonical model identifier string. */
|
|
195
|
+
readonly model: string;
|
|
196
|
+
/** The provider that operates this model. */
|
|
197
|
+
readonly provider: ProviderName;
|
|
198
|
+
/** Quality/cost tier classification. */
|
|
199
|
+
readonly tier: ModelTier;
|
|
200
|
+
/** Cost in USD for one million input (prompt) tokens. */
|
|
201
|
+
readonly input_per_million: number;
|
|
202
|
+
/** Cost in USD for one million output (completion) tokens. */
|
|
203
|
+
readonly output_per_million: number;
|
|
204
|
+
/** Maximum context window in tokens (0 = unknown). */
|
|
205
|
+
readonly context_window: number;
|
|
206
|
+
/** Whether the model accepts image inputs. */
|
|
207
|
+
readonly supports_vision: boolean;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ---------------------------------------------------------------------------
|
|
211
|
+
// BudgetAlert
|
|
212
|
+
// ---------------------------------------------------------------------------
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Immutable record of a fired budget alert event.
|
|
216
|
+
* Maps to AlertEvent dataclass in agent_energy_budget.budget.alerts.
|
|
217
|
+
*/
|
|
218
|
+
export interface BudgetAlert {
|
|
219
|
+
/** The agent whose budget triggered the alert. */
|
|
220
|
+
readonly agent_id: string;
|
|
221
|
+
/** Alert severity level. */
|
|
222
|
+
readonly level: AlertLevel;
|
|
223
|
+
/** Current utilisation percentage at the time of the alert. */
|
|
224
|
+
readonly utilisation_pct: number;
|
|
225
|
+
/** Amount spent in USD at the time of the alert. */
|
|
226
|
+
readonly spent_usd: number;
|
|
227
|
+
/** Budget limit in USD that was approached or exceeded. */
|
|
228
|
+
readonly limit_usd: number;
|
|
229
|
+
/** Budget period label ("daily", "weekly", or "monthly"). */
|
|
230
|
+
readonly period: string;
|
|
231
|
+
/** Human-readable description of the alert condition. */
|
|
232
|
+
readonly message: string;
|
|
233
|
+
/** ISO-8601 UTC timestamp when the alert was fired. */
|
|
234
|
+
readonly fired_at: string;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ---------------------------------------------------------------------------
|
|
238
|
+
// WorkloadForecast (cost prediction)
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Pre-execution cost estimate for an LLM call.
|
|
243
|
+
* Maps to CostEstimate dataclass in agent_energy_budget.estimator.cost_estimator.
|
|
244
|
+
*/
|
|
245
|
+
export interface WorkloadForecast {
|
|
246
|
+
/** Model identifier used for the estimate. */
|
|
247
|
+
readonly model: string;
|
|
248
|
+
/** Estimated number of input (prompt) tokens. */
|
|
249
|
+
readonly estimated_input_tokens: number;
|
|
250
|
+
/** Estimated number of output (completion) tokens. */
|
|
251
|
+
readonly estimated_output_tokens: number;
|
|
252
|
+
/** Estimated total cost in USD. */
|
|
253
|
+
readonly estimated_cost_usd: number;
|
|
254
|
+
/** Confidence score for the estimate in [0.0, 1.0]. */
|
|
255
|
+
readonly confidence: number;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// ---------------------------------------------------------------------------
|
|
259
|
+
// PredictCostRequest
|
|
260
|
+
// ---------------------------------------------------------------------------
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Request payload for predicting the cost of an LLM call before execution.
|
|
264
|
+
*/
|
|
265
|
+
export interface PredictCostRequest {
|
|
266
|
+
/** Agent identifier requesting the cost prediction. */
|
|
267
|
+
readonly agent_id: string;
|
|
268
|
+
/** Model to use for cost estimation. */
|
|
269
|
+
readonly model: string;
|
|
270
|
+
/** Prompt text to estimate input tokens from. */
|
|
271
|
+
readonly prompt_text: string;
|
|
272
|
+
/** Maximum output tokens (used as worst-case upper bound). */
|
|
273
|
+
readonly max_output_tokens?: number;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ---------------------------------------------------------------------------
|
|
277
|
+
// SetBudgetLimitRequest
|
|
278
|
+
// ---------------------------------------------------------------------------
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Request payload for updating budget limits for an agent.
|
|
282
|
+
*/
|
|
283
|
+
export interface SetBudgetLimitRequest {
|
|
284
|
+
/** Agent identifier to update limits for. */
|
|
285
|
+
readonly agent_id: string;
|
|
286
|
+
/** New daily limit in USD (0 = disabled, omit to leave unchanged). */
|
|
287
|
+
readonly daily_limit?: number;
|
|
288
|
+
/** New weekly limit in USD (0 = disabled, omit to leave unchanged). */
|
|
289
|
+
readonly weekly_limit?: number;
|
|
290
|
+
/** New monthly limit in USD (0 = disabled, omit to leave unchanged). */
|
|
291
|
+
readonly monthly_limit?: number;
|
|
292
|
+
/** Degradation strategy to apply on limit breach. */
|
|
293
|
+
readonly degradation_strategy?: DegradationStrategy;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ---------------------------------------------------------------------------
|
|
297
|
+
// ThrottleConfig
|
|
298
|
+
// ---------------------------------------------------------------------------
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Throttling configuration applied when budget limits approach exhaustion.
|
|
302
|
+
* Maps to options in agent_energy_budget.budget.config.
|
|
303
|
+
*/
|
|
304
|
+
export interface ThrottleConfig {
|
|
305
|
+
/** Agent identifier this throttle configuration applies to. */
|
|
306
|
+
readonly agent_id: string;
|
|
307
|
+
/** Whether throttling is currently active for this agent. */
|
|
308
|
+
readonly throttle_active: boolean;
|
|
309
|
+
/** Maximum calls per minute allowed under throttle. */
|
|
310
|
+
readonly max_calls_per_minute: number;
|
|
311
|
+
/** Maximum input tokens per call under throttle. */
|
|
312
|
+
readonly max_input_tokens_per_call: number;
|
|
313
|
+
/** Maximum output tokens per call under throttle. */
|
|
314
|
+
readonly max_output_tokens_per_call: number;
|
|
315
|
+
/** ISO-8601 UTC timestamp when throttling was last applied. */
|
|
316
|
+
readonly throttle_applied_at: string | null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ---------------------------------------------------------------------------
|
|
320
|
+
// UsageReport
|
|
321
|
+
// ---------------------------------------------------------------------------
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Aggregated usage report across all periods for an agent.
|
|
325
|
+
*/
|
|
326
|
+
export interface UsageReport {
|
|
327
|
+
/** Agent identifier this report covers. */
|
|
328
|
+
readonly agent_id: string;
|
|
329
|
+
/** Daily usage snapshot. */
|
|
330
|
+
readonly daily: BudgetStatus;
|
|
331
|
+
/** Weekly usage snapshot. */
|
|
332
|
+
readonly weekly: BudgetStatus;
|
|
333
|
+
/** Monthly usage snapshot. */
|
|
334
|
+
readonly monthly: BudgetStatus;
|
|
335
|
+
/** Total USD spent across all recorded history. */
|
|
336
|
+
readonly lifetime_spend_usd: number;
|
|
337
|
+
/** ISO-8601 UTC timestamp when this report was generated. */
|
|
338
|
+
readonly generated_at: string;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ---------------------------------------------------------------------------
|
|
342
|
+
// API result wrapper (shared pattern)
|
|
343
|
+
// ---------------------------------------------------------------------------
|
|
344
|
+
|
|
345
|
+
/** Standard error payload returned by the agent-energy-budget API. */
|
|
346
|
+
export interface ApiError {
|
|
347
|
+
readonly error: string;
|
|
348
|
+
readonly detail: string;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Result type for all client operations. */
|
|
352
|
+
export type ApiResult<T> =
|
|
353
|
+
| { readonly ok: true; readonly data: T }
|
|
354
|
+
| { readonly ok: false; readonly error: ApiError; readonly status: number };
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"lib": ["ES2020", "DOM"],
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"noImplicitAny": true,
|
|
14
|
+
"strictNullChecks": true,
|
|
15
|
+
"noUnusedLocals": true,
|
|
16
|
+
"noUnusedParameters": true,
|
|
17
|
+
"noImplicitReturns": true,
|
|
18
|
+
"exactOptionalPropertyTypes": true,
|
|
19
|
+
"forceConsistentCasingInFileNames": true,
|
|
20
|
+
"esModuleInterop": true,
|
|
21
|
+
"skipLibCheck": true
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*"],
|
|
24
|
+
"exclude": ["node_modules", "dist"]
|
|
25
|
+
}
|