AI Gateway Module
Connect any OpenAI-compatible SDK to Base44’s managed models viabase44.aiGateway.
Note: Intended for backend functions. It uses your app’s models, billing, and credit quota — there is no API key to manage.
Overview
The AI Gateway exposes an OpenAI-compatible Chat Completions endpoint backed by Base44’s managed models.connection() returns the baseURL and bearer token to
hand to any OpenAI-compatible client (Vercel AI SDK, Mastra, the OpenAI SDK, and
others). Calls are metered against your app’s credit quota, exactly like
integrations.Core.InvokeLLM.
When to use it
The gateway is for code agents — a backend function running an agent loop (tools, multiple steps) the app owns. Compared to the other AI surfaces:Methods
Available in user mode (
base44.aiGateway, the default — runs with the caller’s
permissions) and with the service-role token (base44.asServiceRole.aiGateway) for
genuine cross-user or system work.
Build a code agent
- Guard the function with
await base44.auth.me(), then get the connection withbase44.aiGateway.connection()→{ baseURL, token }. - Point an agent SDK’s OpenAI-compatible provider at it (
baseURL+apiKey: token). - Give the agent tools that read/act on your app via
base44.*, and let it finish by recording its result through a tool.
- Backend function only (
createClientFromRequest(req)). All other backend-function rules (deployment, secrets, error handling) apply — see the functions guide. - Run in the caller’s scope by default. Use
base44.aiGateway.connection()andbase44.entities.*so the agent runs with the calling user’s permissions (RLS applies) and can’t exceed them; guard the function withawait base44.auth.me(). Reach forasServiceRoleonly for genuine cross-user/system work — and then scope tools to trusted context, not agent-chosen inputs (e.g. fixcustomer_emailfrom the request, not an agent parameter), sinceasServiceRoleruns with full access. - Stateless between invocations. Persistent memory means storing and replaying state (e.g. in an entity).
- Use model
automaticunless the task needs a specific model — non-default models use more credits: only when needed, and tell the user. - No streaming.
- Don’t chain
InvokeLLMto fake a tool loop — use a real agent loop. - Always bound the loop.
stopWhenis an OR-list — the first condition to fire wins (mix a step cap likestepCountIs, a finish tool likehasToolCall, or a custom check). Give it room to finish but stop a runaway: every step is another metered model call.
messages:
baseURL and token.
Models
Pass a model id as the client’smodel. Use automatic (the default, cheapest)
unless the task needs a specific model (e.g. claude_sonnet_4_6). Non-default models
cost more credits — use them only when needed, and tell the user.
Notes
- Backend only. The realistic entry point is a backend function via
createClientFromRequest(req).tokenis the current caller’s bearer — the app user’s token forbase44.aiGateway, the service-role token forbase44.asServiceRole.aiGateway, or an empty string when unauthenticated. - No streaming.
- Billing: metered per call against your app’s credit quota (same as InvokeLLM).