UNPKG

ai-patterns

Version:

Production-ready TypeScript patterns to build solid and robust AI applications. Retry logic, circuit breakers, rate limiting, human-in-the-loop escalation, prompt versioning, response validation, context window management, and more—all with complete type

56 lines 1.88 kB
/** * Cost Tracking Pattern * * Monitors and controls AI spending in real-time, preventing budget overruns * and optimizing costs across your application. * * @example * ```typescript * const result = await costTracking({ * execute: async () => { * const { text, usage } = await generateText({ * model: openai('gpt-4-turbo'), * prompt: longPrompt * }); * return { value: text, tokens: usage.totalTokens }; * }, * costPerToken: ModelCost.GPT4_TURBO, * monthlyBudget: 500, * dailyLimit: 50, * onBudgetWarning: (spent, limit) => { * console.warn(`Budget at 80%: $${spent}/$${limit}`); * }, * tags: { feature: 'chatbot', userId: 'user-123' } * }); * ``` */ import type { CostTrackingConfig, CostResult, CostStorage } from "../types/cost-tracking"; /** * In-memory cost storage implementation using GlobalStorage */ declare class InMemoryCostStorage implements CostStorage { private storage; private readonly namespace; private initialized; constructor(); private ensureInitialized; getSpent(period: "monthly" | "daily" | "hourly"): Promise<number>; addSpent(period: "monthly" | "daily" | "hourly", amount: number): Promise<void>; resetSpent(period: "monthly" | "daily" | "hourly"): Promise<void>; } /** * Execute an operation with cost tracking */ export declare function costTracking<TResult = any>(config: CostTrackingConfig<TResult>): Promise<CostResult<TResult>>; /** * Create a cost tracking wrapper function */ export declare function createCostTracker<TResult = any>(baseConfig: Omit<CostTrackingConfig<TResult>, "execute">): (execute: () => Promise<{ value: TResult; tokens?: number; }>) => Promise<CostResult<TResult>>; /** * Export storage for testing and custom implementations */ export { InMemoryCostStorage }; //# sourceMappingURL=cost-tracking.d.ts.map