@saplings/ai
v0.2.0
Published
Provider-neutral generation orchestration
Readme
@saplings/ai
Provider-neutral generation orchestration with tenant authorization and budget reservation.
Usage
import { createGenerationClient } from '@saplings/ai';
const client = createGenerationClient({ provider, budget, authorize, models: ['approved-model'] });
await client.generate({ tenantId: 'tenant-a', model: 'approved-model', prompt: 'Summarize', maxOutputTokens: 100 });Supply a trusted server-side tenant context and an atomic budget implementation. Provider timeouts are cooperative. Failed or ambiguous requests retain their reservation for reconciliation; do not blindly retry billable operations. The byte-based input bound is not a model tokenizer. Provider-specific adapters, streaming and real cost accounting remain separate requirements.
Structured output
generateStructured(request, parse, signal) parses the provider response as JSON and validates it using the caller's schema function. Usage is settled by generate before parsing, so invalid output cannot bypass billing. This does not enable provider-native constrained decoding or automatically retry invalid output.
OpenAI provider
createOpenAIProvider({ apiKey }) implements the server-side Responses API transport with a fixed HTTPS endpoint, redirect rejection, bounded response decoding and actual usage extraction. Use it as the provider in createGenerationClient; configure authorized models and a durable budget. No live API calls are part of the regression suite. Responses without text may represent refusals or incomplete generation; structured parsing can reject them after usage settlement. Do not expose API credentials to the browser.
