Skip to content

AI Integration

Butterbase includes a built-in AI model gateway that lets your app call large language models through an OpenAI-compatible API. You can use the platform’s shared key or bring your own key (BYOK) for direct billing with the model provider.

Your app sends chat completion or embedding requests to Butterbase, which proxies them to the model provider (via OpenRouter). Usage cost is tracked automatically and counted against your plan’s AI credits allowance.

POST /v1/{app_id}/chat/completions
Authorization: Bearer {token}
{
"model": "anthropic/claude-3.5-sonnet",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "What is Butterbase?" }
],
"max_tokens": 500,
"temperature": 0.7
}

Streaming: Set "stream": true to receive server-sent events.

Generate vector embeddings for semantic search, clustering, and other ML tasks:

POST /v1/{app_id}/embeddings
Authorization: Bearer {token}
{
"model": "openai/text-embedding-3-small",
"input": "What is Butterbase?",
"encoding_format": "float"
}

Batch input: Pass an array of strings:

{
"model": "openai/text-embedding-3-small",
"input": ["first text", "second text", "third text"]
}
ModelIDDimensions
Text Embedding 3 Smallopenai/text-embedding-3-small1536
Text Embedding 3 Largeopenai/text-embedding-3-large3072
Text Embedding Ada 002openai/text-embedding-ada-0021536

Butterbase supports any model available through OpenRouter:

ModelID
Claude 3.5 Sonnetanthropic/claude-3.5-sonnet
Claude 3 Opusanthropic/claude-3-opus
Claude 3 Haikuanthropic/claude-3-haiku
GPT-4 Turboopenai/gpt-4-turbo
GPT-4openai/gpt-4
GPT-3.5 Turboopenai/gpt-3.5-turbo
Llama 3.1 70Bmeta-llama/llama-3.1-70b-instruct
Llama 3.1 8Bmeta-llama/llama-3.1-8b-instruct

The full list is available at openrouter.ai/models.

Configure AI settings per app:

PUT /v1/{app_id}/ai/config
{
"defaultModel": "anthropic/claude-3.5-sonnet",
"byokKey": "sk-or-...",
"maxTokensPerRequest": 4096,
"allowedModels": ["anthropic/claude-3.5-sonnet", "anthropic/claude-3-haiku"]
}
SettingDescription
defaultModelModel used when none is specified
byokKeyYour own OpenRouter API key (encrypted at rest)
maxTokensPerRequestMaximum tokens per request (1–100,000)
allowedModelsRestrict which models can be used

By default, requests use the platform’s shared key and count against your plan’s AI credits. With BYOK:

  • Requests are billed directly to your OpenRouter account
  • Usage is not counted against your Butterbase quota
  • Your key is encrypted at rest
GET /v1/{app_id}/ai/usage?startDate=2026-01-01&endDate=2026-01-31

Returns total tokens, cost, and breakdown by model.

The runtime auto-injects BUTTERBASE_APP_ID and BUTTERBASE_API_URL — you only need to supply your API key via envVars:

export default async function handler(req: Request, ctx: any): Promise<Response> {
const { BUTTERBASE_APP_ID, BUTTERBASE_API_URL, BUTTERBASE_API_KEY } = ctx.env;
const aiResponse = await fetch(`${BUTTERBASE_API_URL}/v1/${BUTTERBASE_APP_ID}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${BUTTERBASE_API_KEY}`
},
body: JSON.stringify({
model: 'anthropic/claude-3-haiku',
messages: [{ role: 'user', content: 'Summarize this text: ...' }],
max_tokens: 200
})
});
const result = await aiResponse.json();
return new Response(JSON.stringify(result), {
headers: { 'Content-Type': 'application/json' }
});
}
PlanAI creditsResets?
Free$0.10No — lifetime allowance
Pro$10.00/mo (then $0.10/credit overage)Yes — resets each billing period
EnterpriseUnlimited

BYOK usage is not counted against these limits.