Skip to content

Using AI Models

Last updated View as MarkdownAgent setup

Agents can call AI models from any provider. Workers AI is built in and requires no API keys. You can also use OpenAI, Anthropic, Google Gemini, or any service that exposes an OpenAI-compatible API.

The AI SDK provides a unified interface across all of these providers, and is what AIChatAgent and the starter template use under the hood. You can also use the model routing features in AI Gateway to route across providers, eval responses, and manage rate limits.

Calling AI Models

You can call models from any method within an Agent, including from HTTP requests using the onRequest handler, when a scheduled task runs, when handling a WebSocket message in the onMessage handler, or from any of your own methods.

Agents can call AI models on their own — autonomously — and can handle long-running responses that take minutes (or longer) to respond in full. If a client disconnects mid-stream, the Agent keeps running and can catch the client up when it reconnects.

Streaming over WebSockets