AI SDK v6 provider for OpenCode via @opencode-ai/sdk
npm install ai-sdk-provider-opencode-sdk> Latest Release: Version 1.x supports AI SDK v6. For AI SDK v5 support, use the ai-sdk-v5 tag (0.x.x).
A community provider for the Vercel AI SDK that enables using AI models through OpenCode and the @opencode-ai/sdk. OpenCode is a terminal-based AI coding assistant that supports multiple providers (Anthropic, OpenAI, Google, and more).
This provider enables you to use OpenCode's AI capabilities through the familiar Vercel AI SDK interface, supporting generateText(), streamText(), streamObject(), and structured output via generateText() with Output.object().
| Provider Version | AI SDK Version | NPM Tag | Status | Branch |
| ---------------- | -------------- | ----------- | ----------- | ---------------------------------------------------------------------------------------- |
| 1.x.x | v6 | latest | Stable | main |
| 0.x.x | v5 | ai-sdk-v5 | Maintenance | ai-sdk-v5 |
For AI SDK v6 (recommended):
``bash`
npm install ai-sdk-provider-opencode-sdk ai@^6.0.0
For AI SDK v5:
`bash`
npm install ai-sdk-provider-opencode-sdk@ai-sdk-v5 ai@^5.0.0
This package is compatible with Zod 3 and Zod 4 (aligned with ai):
`bashWith Zod 3
npm install ai-sdk-provider-opencode-sdk ai zod@^3.25.76
Prerequisites
- Node.js >= 18
- OpenCode CLI installed (
npm install -g opencode)
- Valid API keys configured in OpenCode for your preferred providersQuick Start
`typescript
import { generateText } from "ai";
import { opencode } from "ai-sdk-provider-opencode-sdk";const result = await generateText({
model: opencode("anthropic/claude-opus-4-5-20251101"),
prompt: "What is the capital of France?",
});
console.log(result.text);
`Usage
$3
`typescript
import { createOpencode } from "ai-sdk-provider-opencode-sdk";// Default provider (auto-starts server)
const opencode = createOpencode();
// With custom settings
const opencode = createOpencode({
hostname: "127.0.0.1",
port: 4096,
autoStartServer: true,
serverTimeout: 10000,
defaultSettings: {
agent: "build",
sessionTitle: "My Session",
},
});
`$3
Models are specified in
providerID/modelID format:`typescript
// Anthropic models (Claude 4.5 series)
opencode("anthropic/claude-sonnet-4-5-20250929");
opencode("anthropic/claude-haiku-4-5-20251001");
opencode("anthropic/claude-opus-4-5-20251101");// OpenAI models (GPT-5.1 series)
opencode("openai/gpt-5.1");
opencode("openai/gpt-5.1-codex");
opencode("openai/gpt-5.1-codex-mini");
opencode("openai/gpt-5.1-codex-max");
// Google Gemini models
opencode("google/gemini-3-pro-preview");
opencode("google/gemini-2.5-flash");
opencode("google/gemini-2.5-pro");
opencode("google/gemini-2.0-flash");
`$3
`typescript
import { streamText } from "ai";const result = streamText({
model: opencode("anthropic/claude-opus-4-5-20251101"),
prompt: "Write a haiku about coding.",
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
`$3
`typescript
import { generateText, type ModelMessage } from "ai";const messages: ModelMessage[] = [
{ role: "user", content: "My name is Alice." },
{ role: "assistant", content: "Hello Alice! How can I help you today?" },
{ role: "user", content: "What is my name?" },
];
const result = await generateText({
model: opencode("anthropic/claude-opus-4-5-20251101"),
messages,
});
`$3
OpenCode supports different agents for different tasks:
`typescript
const model = opencode("anthropic/claude-opus-4-5-20251101", {
agent: "build", // or 'plan', 'general', 'explore'
});
`$3
Sessions maintain conversation context:
`typescript
const model = opencode("anthropic/claude-opus-4-5-20251101", {
sessionTitle: "Code Review Session",
});// First call creates a session
const result1 = await generateText({ model, prompt: "Review this code..." });
// Subsequent calls reuse the same session
const result2 = await generateText({ model, prompt: "What did you find?" });
// Get session ID from metadata
const sessionId = result1.providerMetadata?.opencode?.sessionId;
// Resume a specific session
const resumeModel = opencode("anthropic/claude-opus-4-5-20251101", {
sessionId: sessionId,
});
`$3
OpenCode executes tools server-side. You can observe tool execution but cannot provide custom implementations:
`typescript
import { streamText } from "ai";const result = streamText({
model: opencode("anthropic/claude-opus-4-5-20251101"),
prompt: "List files in the current directory.",
});
for await (const part of result.fullStream) {
if (part.type === "tool-call") {
console.log(
Tool: ${part.toolName});
console.log(Input: ${JSON.stringify(part.input, null, 2)});
}
if (part.type === "tool-result") {
console.log(Result: ${part.result});
}
}
`Feature Support
| Feature | Support | Notes |
| ------------------------ | ---------- | --------------------------------------------------- |
| Text generation | ✅ Full |
generateText(), streamText() |
| Streaming | ✅ Full | Real-time SSE streaming |
| Multi-turn conversations | ✅ Full | Session-based context |
| Tool observation | ✅ Full | See tool execution |
| Reasoning/thinking | ✅ Full | ReasoningPart support |
| Model selection | ✅ Full | Per-request model |
| Agent selection | ✅ Full | build, plan, general, explore |
| Abort/cancellation | ✅ Full | AbortSignal support |
| Image input (base64) | ⚠️ Partial | Data URLs only |
| Image input (URL) | ❌ None | Not supported |
| Structured output (JSON) | ⚠️ Partial | Output.object() / streamObject() (prompt-based) |
| Custom tools | ❌ None | Server-side only |
| temperature/topP/topK | ❌ None | Provider defaults |
| maxTokens | ❌ None | Agent config |Provider Settings
`typescript
interface OpencodeProviderSettings {
hostname?: string; // Default: '127.0.0.1'
port?: number; // Default: 4096
baseUrl?: string; // Override full URL
autoStartServer?: boolean; // Default: true
serverTimeout?: number; // Default: 10000
defaultSettings?: OpencodeSettings;
}
`Model Settings
`typescript
interface OpencodeSettings {
sessionId?: string; // Resume session
createNewSession?: boolean; // Force new session
sessionTitle?: string; // Title for new sessions
agent?: string; // Agent name
systemPrompt?: string; // Override system prompt
tools?: Record; // Enable/disable tools
cwd?: string; // Working directory
logger?: Logger | false; // Logging
verbose?: boolean; // Debug logging
}
`Error Handling
The provider converts OpenCode errors to AI SDK error types:
`typescript
import {
isAuthenticationError,
isTimeoutError,
} from "ai-sdk-provider-opencode-sdk";try {
const result = await generateText({ model, prompt: "..." });
} catch (error) {
if (isAuthenticationError(error)) {
console.error("Check your API keys in OpenCode");
} else if (isTimeoutError(error)) {
console.error("Request timed out");
}
}
`Cleanup
Always dispose of the provider when done to stop the managed server:
`typescript
const opencode = createOpencode();// ... use the provider ...
// Clean up
await opencode.dispose?.();
``MIT