Skip to main content
Glama

ultra-budget

Set and monitor conversation budgets to control costs, tokens, and duration for AI sessions. Manage spending limits and track usage across multiple AI providers.

Instructions

Set and monitor conversation budgets for cost and token control

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
actionYesAction to perform
sessionIdYesSession ID to manage budget for
maxTokensNoMaximum tokens allowed for the session
maxCostUsdNoMaximum cost in USD allowed for the session
maxDurationMsNoMaximum duration in milliseconds allowed for the session

Implementation Reference

  • src/server.ts:994-1000 (registration)
    Registration of the ultra-budget tool using server.registerTool, specifying title, description, ZenBudgetSchema for input validation, and delegating to handleBudget function.
    server.registerTool("ultra-budget", { title: "Zen Budget", description: "Set and monitor conversation budgets for cost and token control", inputSchema: ZenBudgetSchema.shape, }, async (args) => { return await handleBudget(args) as any; });
  • ZenBudgetSchema: Zod schema defining the input structure and validation for the ultra-budget tool, including actions (set/get/check), sessionId, and optional budget limits.
    const ZenBudgetSchema = z.object({ action: z.enum(["set", "get", "check"]).describe("Action to perform"), sessionId: z.string().describe("Session ID to manage budget for"), maxTokens: z.number().optional().describe("Maximum tokens allowed for the session"), maxCostUsd: z.number().optional().describe("Maximum cost in USD allowed for the session"), maxDurationMs: z.number().optional().describe("Maximum duration in milliseconds allowed for the session"), });
  • Core handler function for ultra-budget tool. Processes 'set', 'get', and 'check' actions using conversationMemory to manage session budgets for tokens, cost (USD), and duration (ms). Returns formatted text responses with status and limits.
    export async function handleBudget(args: any) { const { action, sessionId, maxTokens, maxCostUsd, maxDurationMs } = args; switch (action) { case 'set': { const budget = await conversationMemory.setBudget(sessionId, maxTokens, maxCostUsd, maxDurationMs); return { content: [ { type: 'text', text: `## Budget Set\n\nSession: ${sessionId}\nMax Tokens: ${maxTokens || 'None'}\nMax Cost: $${maxCostUsd || 'None'}\nMax Duration: ${maxDurationMs || 'None'}ms` } ] }; } case 'get': { const context = await conversationMemory.getConversationContext(sessionId); const budget = context.budget; return { content: [ { type: 'text', text: `## Budget Status\n\nSession: ${sessionId}\n\n**Limits:**\n- Max Tokens: ${budget?.maxTokens || 'None'}\n- Max Cost: $${budget?.maxCostUsd || 'None'}\n- Max Duration: ${budget?.maxDurationMs || 'None'}ms\n\n**Usage:**\n- Used Tokens: ${budget?.usedTokens || 0}\n- Used Cost: $${budget?.usedCostUsd || 0}\n- Used Duration: ${budget?.usedDurationMs || 0}ms` } ] }; } case 'check': { const limits = await conversationMemory.checkBudgetLimits(sessionId); const status = limits.withinLimits ? '✅ Within Limits' : '⚠️ Limits Exceeded'; const details = [ `Token Limit: ${limits.tokenLimitExceeded ? '❌ Exceeded' : '✅ OK'}`, `Cost Limit: ${limits.costLimitExceeded ? '❌ Exceeded' : '✅ OK'}`, `Duration Limit: ${limits.durationLimitExceeded ? '❌ Exceeded' : '✅ OK'}` ].join('\n'); return { content: [ { type: 'text', text: `## Budget Check\n\nSession: ${sessionId}\nStatus: ${status}\n\n${details}` } ] }; } default: throw new Error(`Unknown action: ${action}`); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/RealMikeChong/ultra-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server