Documentation/Core Concepts/Duck-Typed Database Sinks
Duck-Typed Database Sinks (Supabase & PostgreSQL)
Save full token breakdown and USD costs into your database with Zero Added Latency and Zero Blast Radius.
---
1-Line Supabase Integration
Simply pass your initialized Supabase client:
typescript
import { streamText } from 'ai';import { vibezcheck } from 'vibezcheck';import { supabase } from '@/lib/supabase';export async function POST(req: Request) { const { messages, userId } = await req.json(); return streamText({ model: vibezcheck('openai/gpt-', { customer: userId, database: supabase, // 👈 Auto-inserts to `vibez_usage` table in background }), messages, }).toDataStreamResponse();}---
Custom Table or Callback
typescript
// Custom table name:database: { client: supabase, table: 'custom_ai_usage' }// Or inline callback:database: async (event) => { await db.insert(event);}---
Zero Blast Radius Protection
All database writes are executed inside isolated background microtasks (after() / waitUntil()). If your database experiences a connection timeout or network hiccup, the user's streaming response is never interrupted.
---
PostgreSQL & Supabase Table Schema
sql
CREATE TABLE IF NOT EXISTS vibez_usage ( id BIGSERIAL PRIMARY KEY, customer_id TEXT, user_id TEXT, model TEXT NOT NULL, provider TEXT NOT NULL DEFAULT 'openai', input_tokens INTEGER NOT NULL DEFAULT 0, output_tokens INTEGER NOT NULL DEFAULT 0, total_tokens INTEGER NOT NULL DEFAULT 0, cached_tokens INTEGER, reasoning_tokens INTEGER, cost_usd NUMERIC(10, 6) NOT NULL DEFAULT 0, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), metadata JSONB DEFAULT '{}'::jsonb);CREATE INDEX IF NOT EXISTS idx_vibez_usage_customer ON vibez_usage(customer_id);CREATE INDEX IF NOT EXISTS idx_vibez_usage_created ON vibez_usage(created_at DESC);Previous
Core Concepts
Profit Margins & Sane Defaults
Next Guide
Core Concepts
Prepaid vs. Postpaid Billing
💡 Tip: Use Left / Right arrow keys to navigate guidesPage 8 of 18