Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content
Framework integrations

Vercel AI SDK Integration

Middleware that wraps any AI SDK model with automatic memory injection and interaction capture.

@mubit-ai/ai-sdk provides mubitMemoryMiddleware, a middleware factory you pass to Vercel AI SDK's wrapLanguageModel(). The wrapped model auto-injects relevant lessons before each call, captures the interaction, and reflects on session end.

npm install @mubit-ai/ai-sdk

Minimal usage

vercel_ai_sdk_minimal.mjs
import { wrapLanguageModel, generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { mubitMemoryMiddleware } from "@mubit-ai/ai-sdk";
 
const model = wrapLanguageModel({
  model: openai("gpt-4o"),
  middleware: mubitMemoryMiddleware({
    apiKey: process.env.MUBIT_API_KEY,
    sessionId: "session-1",
    agentId: "support-agent",
  }),
});
 
const { text } = await generateText({
  model,
  prompt: "How do I reset my password?",
});
// Lessons auto-injected before call; interaction auto-captured after.

Full example: FAQ bot with cross-session learning

A multi-session FAQ bot with a knowledge-base tool. Session 2 benefits from a lesson captured in Session 1.

faq_bot/index.mjs
import { generateText, tool, wrapLanguageModel } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
import { mubitMemoryMiddleware, MubitClient } from "@mubit-ai/ai-sdk";
 
const mubitClient = new MubitClient({ apiKey: process.env.MUBIT_API_KEY });
 
const lookupKnowledgeBase = tool({
  description: "Search the knowledge base for help articles",
  parameters: z.object({ query: z.string() }),
  execute: async ({ query }) => findBestArticle(query),
});
 
// Session 1: answer a couple of FAQ questions
const model1 = wrapLanguageModel({
  model: openai("gpt-4o-mini"),
  middleware: mubitMemoryMiddleware({
    sessionId: "faq-session-1",
    agentId: "faq-bot",
    mubitClient,
  }),
});
 
for (const question of [
  "How do I reset my password?",
  "What are system requirements?",
]) {
  const { text } = await generateText({
    model: model1,
    tools: { lookupKnowledgeBase },
    maxSteps: 3,
    prompt: question,
  });
  console.log(`Q: ${question}\nA: ${text}`);
}
 
// Ingest a lesson between sessions
await mubitClient.memory.remember(
  "Users who ask about password reset often also need 2FA help.",
  { kind: "lesson", runId: "faq-session-1", idempotencyKey: "faq-2fa-cross-sell" },
);
 
// Session 2: new session benefits from memory
const model2 = wrapLanguageModel({
  model: openai("gpt-4o-mini"),
  middleware: mubitMemoryMiddleware({
    sessionId: "faq-session-2",
    agentId: "faq-bot",
    mubitClient,
  }),
});
 
const { text } = await generateText({
  model: model2,
  tools: { lookupKnowledgeBase },
  maxSteps: 3,
  prompt: "I'm having trouble logging in",
});
// Response references password / 2FA from Session 1.

Extended Mubit features

The middleware focuses on inject + capture. The full MAS surface is on the MubitClient you pass in (the SDK Client re-exported by @mubit-ai/ai-sdk; the middleware constructs one internally if you do not pass your own — pass your own to share it across middlewares). The runId option below is the middleware's sessionId:

await mubitClient.agents.register({ agentId: agent, role, runId, readScopes, writeScopes });
await mubitClient.agents.handoffs.create({ runId, taskId, fromAgentId, toAgentId, content, requestedAction });
await mubitClient.agents.handoffs.feedback({ runId, handoffId, verdict, fromAgentId });
await mubitClient.checkpoint({ session_id: runId, context_snapshot: contextSnapshot, label });
await mubitClient.outcomes.record({ runId, referenceId, good, rationale });
await mubitClient.lessons.reflect({ runId });
await mubitClient.lessons.strategies({ runId, lessonTypes, maxStrategies });
await mubitClient.memory.diagnose(errorText, { runId });
await mubitClient.memory.archive({ content, runId, artifactKind });
await mubitClient.memory.dereference(referenceId, { runId });
await mubitClient.memory.context(query, { runId, budget });

Gotchas

  • maxSteps matters: tool-calling roundtrips count as separate model calls. The middleware injects lessons for each.
  • Streaming responses (streamText) are supported; the assembled output is captured on stream completion.
  • Reflection on session end is opt-in. Pass autoReflect: true to the middleware, or call mubitClient.lessons.reflect({ runId }) yourself when the session ends.
  • No CommonJS: @mubit-ai/ai-sdk ships ESM-only.

Version compatibility

@mubit-ai/ai-sdkmubit-sdk (Node)ai
0.5.x>= 0.6.0>= 4.0.0