Spaces:
Running
Running
File size: 1,624 Bytes
249c849 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | /**
* Token Cost Guard SDK (Typescript)
* Usage: npm install token-cost-guard
*/
export interface LogEntry {
step: string;
agent: string;
prompt_tokens: number;
completion_tokens: number;
model: string;
input: string;
output: string;
}
export class TokenCostGuard {
private apiKey: string;
private endpoint: string = "https://your-domain.com/api/v1";
constructor(apiKey: string) {
if (!apiKey.startsWith("TCG-")) {
throw new Error("Invalid API Key format");
}
this.apiKey = apiKey;
}
/**
* Optimize a set of agent logs and return the execution plan
*/
async optimize(logs: LogEntry[]) {
const response = await fetch(`${this.endpoint}/optimize`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.apiKey}`
},
body: JSON.stringify({ logs })
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.error || "Optimization failed");
}
return response.json();
}
/**
* Get current usage and savings stats
*/
async getStats() {
const response = await fetch(`${this.endpoint}/usage`, {
headers: {
"Authorization": `Bearer ${this.apiKey}`
}
});
return response.json();
}
}
// Example Integration for AutoGen / LangChain
/*
const guardian = new TokenCostGuard("TCG-PRO-xxxx-xxxx");
async function onWorkflowComplete(logs) {
const plan = await guardian.optimize(logs);
console.log("Optimized Plan Readiness:", plan.actions);
// Apply plan.executable_patch to the next run
}
*/
|