AI SDK
Track model costs and priced tools in an agent.
The orbytelabs/ai-sdk integration bills model usage and priced tools from your
deployed catalog. This example uses AI Gateway and a weather tool with demo data.
Install
npm install orbytelabs ai@^7.0.138 zodTo run the TypeScript example locally:
npm install --save-dev tsxDefine a tool
Create tools.ts. Replace the demo result with your own weather provider when
connecting it to your application.
import { tool } from "ai";
import z from "zod";
export const tools = {
weather: tool({
description: "Get demo weather conditions for a location",
inputSchema: z.object({ location: z.string() }),
execute: async ({ location }) => ({
location,
temperature: 28,
condition: "sunny",
demo: true,
}),
}),
};Define billing in the catalog
Create orbyte.config.ts alongside the tool:
import { credits, defineConfig } from "orbytelabs";
import { aiSdkFeatures } from "orbytelabs/ai-sdk";
import { tools } from "./tools";
export default defineConfig({
features: aiSdkFeatures(credits.markup("25%"), tools, {
weather: credits("0.02"),
}),
});This creates inference features and count features for priced tools. Deploy the catalog, then grant credits to fund inference and priced tools. Tool definitions contain no billing metadata. Omitted or zero-priced tools execute without billing.
npx orbytelabs login
npx orbytelabs link
npx orbytelabs deploy
npx orbytelabs grant 5 --identity customer_123 \
--idempotency-key ai_example_customer_123
npx orbytelabs api-key create --name "AI example"Save the key's one-time secret and your AI Gateway key in .env. Keep this file
out of version control.
ORBYTE_API_KEY=your-orbyte-api-key
AI_GATEWAY_API_KEY=your-ai-gateway-api-keyRun the agent
Create agent.mts:
import { TrackedToolLoopAgent } from "orbytelabs/ai-sdk";
import { tools } from "./tools";
const agent = new TrackedToolLoopAgent(
{
model: "openai/gpt-6-luna",
tools,
instructions: "Use the weather tool and explain that it returns demo data.",
},
{ identity: "customer_123" },
);
const result = await agent.generate({ prompt: "What's the weather in Paris?" });
console.log(result.text);node --env-file=.env --import tsx agent.mtsIn your application, use the authenticated customer's identity instead of
customer_123. Choose any model available through your AI Gateway account.
The agent reads prices from the deployed catalog. It imports no pricing config.
With AI Gateway, provider USD cost maps to one credit per dollar before markup.
A $0.004 step with 25% markup costs 0.005 credits. Provider costs round up to nano
dollars before pricing, and charges round up to nano credits after markup.
A $0.0000236 step with 25% markup consumes exactly 0.0000295 credits.
The provider expense is recorded as costUsdInNanos; the $0.004 expense is
4_000_000n. Direct providers supply an inferenceCost(step) callback returning USD.
Billing boundaries
The adapter checks access, records completed model steps and successful priced
tools, and surfaces tracking failures. Checks reserve no funds. Consume streams
to completion; interrupted work may not report costs. Free tools create no billing
event. Zero-cost model steps record an event with costUsdInNanos: 0n and zero usage.
For model-specific pricing, pass aiSdkFeatures a map of model IDs to cost rules
and an optional default. A configured model rule takes precedence; a denied
rule never falls back to the default.
Use native AI SDK functions
For generateText or streamText, create one BillingTelemetry per generation:
import { generateText, stepCountIs } from "ai";
import { BillingTelemetry } from "orbytelabs/ai-sdk";
import { tools } from "./tools";
using billing = new BillingTelemetry({ identity: "customer_123" });
const result = await generateText({
model: "openai/gpt-6-luna",
tools,
prompt: "What's the weather in Paris?",
stopWhen: stepCountIs(5),
telemetry: { integrations: billing },
});
console.log(result.text);The using scope surfaces billing failures when it exits. Without using, call
billing.assert() after consuming the result, including when generation fails.
For streaming, consume the stream inside the scope. Returning an unconsumed
HTTP stream disposes billing too early. The tracked agent manages this lifetime
for you.
