Cogitator
Integrations

Vercel AI SDK

Bidirectional bridge between Cogitator and the Vercel AI SDK 4, 5, 6 and 7 -- use Cogitator agents as AI SDK models or use AI SDK models inside Cogitator.

Overview

The @cogitator-ai/ai-sdk package connects the two ecosystems in both directions:

  • Cogitator as model -- expose Cogitator agents as AI SDK language models, so you can use them with generateText, streamText and structured output.
  • AI SDK as backend -- wrap any AI SDK model as a Cogitator LLMBackend.
  • Tools -- convert tools in both directions.
pnpm add @cogitator-ai/ai-sdk @cogitator-ai/core ai

Supported AI SDK Versions

ai is a peer dependency: ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0.

AI SDKAccepted model specsDefault cogitatorModel() specfromAISDK() input
ai@4v1v1LanguageModelV1
ai@5v2v2LanguageModelV2
ai@6v2, v3v3LanguageModelV3
ai@7v2, v3, v4v4LanguageModelV4

Specification Auto-Detection

Without a specificationVersion option, the model spec follows the installed ai package:

  • At runtime ai/package.json is resolved from process.cwd(), then from @cogitator-ai/ai-sdk itself (ESM-only installs that hide package.json behind exports are handled). The result is cached. If ai cannot be resolved, 'v2' is used -- ai@5, ai@6 and ai@7 accept it.
  • In TypeScript the default return type is derived from the installed ai package's LanguageModel type with the same mapping, so code written for any major type-checks unchanged.

Pass specificationVersion: 'v1' | 'v2' | 'v3' | 'v4' to override it, e.g. when several ai majors are installed. AI SDK 6 and 7 accept v2 models only in a compatibility mode that logs a warning on every call, which the detected v3 / v4 defaults avoid.

Cogitator as AI SDK Model

cogitatorModel

Pass a Cogitator instance and an Agent to get an AI SDK language model:

import { generateText, streamText } from 'ai';
import { Cogitator, Agent } from '@cogitator-ai/core';
import { cogitatorModel } from '@cogitator-ai/ai-sdk';

const cog = new Cogitator({
  llm: {
    defaultProvider: 'openai',
    providers: { openai: { apiKey: process.env.OPENAI_API_KEY! } },
  },
});

const agent = new Agent({
  name: 'researcher',
  model: 'openai/gpt-6.1-sol',
  instructions: 'You are an expert researcher. Always cite sources.',
  tools: [webSearch, readUrl],
});

const { text } = await generateText({
  model: cogitatorModel(cog, agent, { temperature: 0.7 }),
  prompt: 'What are the latest advances in quantum computing?',
});

const result = streamText({
  model: cogitatorModel(cog, agent, { specificationVersion: 'v2' }),
  prompt: 'Summarize them in three bullet points',
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}

The first model uses the auto-detected spec; the second overrides it. On ai@4 the default is a LanguageModelV1.

How an Agent Behaves as a Model

  • One model call runs the whole agent loop, including the agent's own tool calls. Its finishReason says how the run ended: stop for an answer, length for an answer cut at the token limit, content-filter for a withheld answer (raw content_filter or refusal), other with raw iteration-limit when tool calls used up maxIterations, and tool-calls for a run that waits for tool approvals (below). providerMetadata.cogitator carries truncated, blocked and iterationLimitReached as well.

  • generateText and streamText get the same text: what the agent writes before a tool call comes before the call in both. With structured output only the final answer is text, so a preamble never breaks the parsed object.

  • The agent's tool calls are reported as provider-executed tool calls with results, so the AI SDK never runs them a second time:

    • v3 / v4 models always report them in toolCalls / toolResults and as tool-call / tool-result stream parts.
    • v2 models report them only for tools that you also pass in tools, because ai@5 cannot handle provider-executed calls of undeclared tools.
    • All models list them in providerMetadata.cogitator.toolCalls next to runId, threadId, agentId, model, cost and duration.
  • The trailing user messages of the prompt are the agent input, and the turns before them reach the run as its history, as messages (text, tool calls and their results), not as a transcript. System messages, images and files are not forwarded (the agent instructions are the system prompt) and produce a warning. A prompt that does not end with a user message is refused, unless it answers tool approvals.

  • providerOptions.cogitator takes threadId, userId and saveHistory (providerMetadata.cogitator on ai@4). With a threadId the memory thread is the history: only the new turn of the prompt is used and it is saved there. Without one the prompt is the whole conversation and the turn is saved only with saveHistory: true.

    await streamText({
      model: cogitatorModel(cog, support),
      messages,
      providerOptions: { cogitator: { threadId: chatId, userId: session.userId } },
    });
  • v4 models report what happens inside the run as custom parts: cogitator.handoff (from, to, reason) and cogitator.model-fallback (from, to, error), details in providerMetadata.cogitator, in the content and the stream in order.

  • temperature, topP, maxOutputTokens (maxTokens on ai@4) and stopSequences override the agent configuration for that call. abortSignal and cancelling a stream abort the run.

  • The reasoning option of v4 models overrides the effort of the agent's reasoning. With reasoning: { summary: true } the agent's summary comes back as reasoning content and reasoning-* stream parts.

  • Structured output (Output.object, generateObject, ai@4 object modes) switches the agent to JSON mode and adds the JSON schema to the input.

  • AI SDK tools that the agent does not own cannot be called by the agent and produce a warning.

  • toolChoice: 'none' runs the agent without its tools. required and a named tool of the agent are forced on the run until the model makes a call, then it goes back to auto. A named tool the agent does not have produces an unsupported-setting warning.

  • When the agent calls a tool that needs approval (requiresApproval), its run pauses and the call never passes for an answer. v3 and v4 models (ai@6, ai@7) end the turn with finishReason: 'tool-calls' and a tool-approval-request for each waiting call (a provider-executed tool-call comes with it). Answer with a tool-approval-response (providerExecuted: true, which convertToModelMessages sets for UI messages) as the last message of the next prompt and the run resumes: approved calls run, denied ones are declined with your reason. v1 and v2 models cannot ask, so they finish with finishReason: 'other' and a warning, and providerMetadata.cogitator holds status: 'paused', the threadId and pendingApprovals to resume with cogitator.resume(agent, threadId, { decisions }).

const paused = await generateText({ model: cogitatorModel(cog, agent), prompt: 'Refund A-1' });
const request = paused.content.find((part) => part.type === 'tool-approval-request');

if (request?.type === 'tool-approval-request') {
  const done = await generateText({
    model: cogitatorModel(cog, agent),
    messages: [
      { role: 'user', content: 'Refund A-1' },
      ...paused.response.messages,
      {
        role: 'tool',
        content: [
          {
            type: 'tool-approval-response',
            approvalId: request.approvalId,
            approved: true,
            providerExecuted: true,
          },
        ],
      },
    ],
  });
}
import { Output, generateText } from 'ai';
import { z } from 'zod';

const { output } = await generateText({
  model: cogitatorModel(cog, agent),
  output: Output.object({ schema: z.object({ title: z.string(), year: z.number() }) }),
  prompt: 'Name a landmark quantum computing paper',
});

createCogitatorProvider

When you have multiple agents, createCogitatorProvider resolves them by name:

import { generateText } from 'ai';
import { createCogitatorProvider } from '@cogitator-ai/ai-sdk';

const provider = createCogitatorProvider(cog, {
  agents: [researcher, summarizer],
  specificationVersion: 'v2',
});

const { text } = await generateText({
  model: provider('researcher', { temperature: 0.5 }),
  prompt: 'Explain CRISPR gene editing',
});

const { text: summary } = await generateText({
  model: provider.languageModel('summarizer', { maxTokens: 2048 }),
  prompt: text,
});

agents may be an array (keyed by agent name), a Map or a record. An unknown name throws NoSuchModelError with the list of available agents.

The provider has the members of its specification, so createProviderRegistry takes it next to other providers. Its embedding and image models throw NoSuchModelError:

import { createProviderRegistry } from 'ai';

const registry = createProviderRegistry({ cogitator: provider, openai });
const model = registry.languageModel('cogitator:researcher');

toAISDKAgent

With ai@7, toAISDKAgent from @cogitator-ai/ai-sdk/agent turns an agent into an AI SDK agent (agent-v1), a ToolLoopAgent on cogitatorModel(), for createAgentUIStreamResponse and anything else that takes one:

import { toAISDKAgent } from '@cogitator-ai/ai-sdk/agent';

const support = toAISDKAgent(cog, supportAgent);
const result = await support.stream({ prompt: 'Where is order 42?' });

It takes the settings of ToolLoopAgent except the model, instructions and tools, which the Cogitator agent has itself.

Options

OptionTypeDescription
temperaturenumberDefault temperature for the agent
maxTokensnumberDefault max output tokens
topPnumberDefault top-p sampling
specificationVersion'v1' | 'v2' | 'v3' | 'v4'Model spec to implement (cogitatorModel and createCogitatorProvider config), detected from the installed ai package when omitted

AI SDK Models in Cogitator

fromAISDK

Wrap any AI SDK language model -- LanguageModelV1 (ai@4 providers) through LanguageModelV4 (ai@7 providers) -- as a Cogitator LLMBackend:

import { google } from '@ai-sdk/google';
import { fromAISDK } from '@cogitator-ai/ai-sdk';

const backend = fromAISDK(google('gemini-3.5-flash-lite'));

const response = await backend.chat({
  model: 'gemini-3.5-flash-lite',
  messages: [
    { role: 'system', content: 'You are a creative writing assistant.' },
    { role: 'user', content: 'Write a haiku about TypeScript' },
  ],
});

for await (const chunk of backend.chatStream({
  model: 'gemini-3.5-flash-lite',
  messages: [{ role: 'user', content: 'Tell me a story' }],
})) {
  process.stdout.write(chunk.delta.content ?? '');
}

The backend branches on the model's specificationVersion and converts:

  • messages -- text, images (URL or base64), assistant tool calls and tool results as structured prompt parts
  • tools, toolChoice, responseFormat, temperature, topP, maxTokens, stop and the abort signal
  • reasoning.effort -- the reasoning call option of v4 models (max becomes xhigh); for older specifications, set reasoning on the AI SDK provider model
  • responses -- text, reasoning (ChatResponse.reasoning, delta.reasoning), client tool calls (provider-executed calls are skipped), finish reasons and usage including cached, cache-write and reasoning tokens
  • Gemini thought signatures, reasoning before a tool call with its provider metadata (Anthropic thinking signatures) and each tool call's provider metadata, which are replayed on the next turn
  • tool results with images as content outputs with the image, and failed tool calls as error-text

Running Agents on an AI SDK Model

To run Cogitator agents on an AI SDK model, register the backend under a name in llm.backends and point agents at it with name/model:

import { Agent, Cogitator } from '@cogitator-ai/core';

const cog = new Cogitator({
  llm: { backends: { gemini: fromAISDK(google('gemini-3.5-flash-lite')) } },
});

const agent = new Agent({
  name: 'writer',
  model: 'gemini/gemini-3.5-flash-lite',
  instructions: 'You write short poems.',
});

const result = await cog.run(agent, { input: 'A haiku about types' });

Tools, memory, streaming and every other run feature work as with built-in providers. The wrapped model is used whatever model name the request carries; cost is reported only for models the price registry knows.

Tool Conversion

Tools can be converted between the two systems, so you never need to rewrite tool definitions.

Cogitator to AI SDK

import { generateText, stepCountIs } from 'ai';
import { openai } from '@ai-sdk/openai';
import { toAISDKTool, convertToolsToAISDK } from '@cogitator-ai/ai-sdk';

const aiTool = toAISDKTool(calculator);

const { text } = await generateText({
  model: openai('gpt-6.1-sol'),
  tools: convertToolsToAISDK([calculator, webSearch]),
  stopWhen: stepCountIs(5),
  prompt: 'What is 42 * 17?',
});

Converted tools work with the tools option of every AI SDK major:

  • inputSchema (ai@5 – ai@7) is the Cogitator tool's zod schema.
  • parameters (ai@4) is an AI SDK Schema with draft-07 JSON Schema and validation.
  • execute(input, options) builds a Cogitator ToolContext: toolCallId becomes toolCallId (and runId when the context names none), abortSignal becomes signal, and the string fields agentId, runId, threadId, userId, channelType, channelId, topicId and conversationId of the AI SDK tool context (context on ai@7, experimental_context on ai@5 / ai@6) are copied.
  • toModelOutput shows the model a media result as an image instead of base64 JSON.
  • needsApproval mirrors the tool's requiresApproval (true, or a function of the input where a throw counts as yes), so ai@6 and ai@7 ask for approval before they run it.

Running converted tools through Cogitator

Without options the AI SDK calls the tool's execute directly, so the tool runs without the guardrails, the sandbox of a sandboxed tool or tool.timeout, and ai@4 and ai@5, which do not read needsApproval, run a tool that needs approval without asking. Pass your Cogitator as invoker and every call goes through cogitator.invokeTool, the way an agent run executes it:

import { generateText } from 'ai';
import { convertToolsToAISDK } from '@cogitator-ai/ai-sdk';

const { text } = await generateText({
  model,
  tools: convertToolsToAISDK([refund, lookupOrder], {
    invoker: cogitator,
    onApproval: async (request) => ({ approved: await askUser(request) }),
  }),
  prompt: 'Refund order A-1',
});
  • A call the AI SDK asks approval for (the tool's requiresApproval holds for its input) and that the AI SDK approval flow approved (ai@6, ai@7: an approving tool-approval-response for the call in the last message, as the AI SDK reads it) runs without being asked again. In most apps those messages come from the client, so set the AI SDK's experimental_toolApprovalSecret to have it refuse approvals your server did not sign. An approval in the messages for a call the AI SDK never asks about counts for nothing.
  • Any other call that needs approval, every one on ai@4 and ai@5, or one the guardrails want approved, goes to onApproval, then to guardrails.onToolApproval. A call nobody decides fails with Tool "refund" needs approval, and nobody approved this call, a declined one with its reason, and the model reads the error.
  • ai@7 can also decide approval for a whole call with toolApproval, which leaves no approval response in the messages: with an invoker, decide those calls in onApproval.
  • The AI SDK already parsed the arguments with the tool's schema, so they are not parsed again and transforms apply once.

AI SDK to Cogitator

import { tool } from 'ai';
import { z } from 'zod';
import { fromAISDKTool, convertToolsFromAISDK } from '@cogitator-ai/ai-sdk';

const weatherTool = tool({
  description: 'Get current weather for a location',
  inputSchema: z.object({
    location: z.string().describe('City name'),
    unit: z.enum(['celsius', 'fahrenheit']).optional(),
  }),
  execute: async ({ location, unit }) => {
    return { temperature: 22, unit: unit ?? 'celsius', location };
  },
});

const cogWeather = fromAISDKTool(weatherTool, 'get_weather');

const agent = new Agent({
  name: 'weather-bot',
  model: 'openai/gpt-6.1-sol',
  instructions: 'Help users check the weather.',
  tools: [cogWeather],
});

fromAISDKTool reads inputSchema (ai@5+) or parameters (ai@4) and accepts zod 4 schemas, Standard JSON Schemas, jsonSchema() / zodSchema() from any AI SDK major and plain JSON Schema objects. Non-zod schemas are converted to JSON Schema and validated before execute runs. It resolves ai@7 dynamic descriptions, returns the last value of streaming (async function*) tools, and passes the Cogitator context as context / experimental_context. needsApproval becomes requiresApproval, so a Cogitator run pauses before the tool runs. A needsApproval function that answers with a promise counts as "needs approval", since Cogitator decides synchronously. Wrap zod 3 schemas with zodSchema() from ai. The tool name must be one every LLM provider accepts, as for tool(), or fromAISDKTool throws a TypeError (convertToolsFromAISDK names tools by their keys, so a key such as weather.get throws too, normalizeToolName() gives weather_get).

Convert multiple tools at once with convertToolsFromAISDK:

const cogTools = convertToolsFromAISDK({ weather: weatherTool, search: searchTool });

Full Example: Next.js Route with AI SDK 7

app/api/chat/route.ts
import { convertToModelMessages, streamText, type UIMessage } from 'ai';
import { Cogitator, Agent, tool } from '@cogitator-ai/core';
import { cogitatorModel } from '@cogitator-ai/ai-sdk';
import { z } from 'zod';

const cog = new Cogitator({
  llm: {
    defaultProvider: 'anthropic',
    providers: { anthropic: { apiKey: process.env.ANTHROPIC_API_KEY! } },
  },
});

const agent = new Agent({
  name: 'assistant',
  model: 'anthropic/claude-sonnet-5-5',
  instructions: 'You are a helpful coding assistant.',
  tools: [
    tool({
      name: 'lookup_docs',
      description: 'Look up a documentation page',
      parameters: z.object({ topic: z.string() }),
      execute: async ({ topic }) => ({ url: `https://docs.example.com/${topic}` }),
    }),
  ],
});

const model = cogitatorModel(cog, agent);

export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json();

  const result = streamText({
    model,
    messages: await convertToModelMessages(messages),
  });

  return result.toUIMessageStreamResponse();
}

The agent executes lookup_docs itself; the UI stream shows the call and its result as provider-executed tool parts.

On this page