---
title: "Reference: withMastra() | AI SDK"
description: API reference for withMastra(), a function to use Mastra functionality in AI SDK.
packages:
  - "@mastra/ai-sdk"
  - "@mastra/core"
---

import PropertiesTable from "@site/src/components/PropertiesTable";

# withMastra()

Wraps an AI SDK model with Mastra processors and/or memory.

## Usage example

```typescript title="src/example.ts"
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import { withMastra } from '@mastra/ai-sdk';
import type { Processor } from '@mastra/core/processors';

const loggingProcessor: Processor<'logger'> = {
  id: 'logger',
  async processInput({ messages }) {
    console.log('Input:', messages.length, 'messages');
    return messages;
  },
};

const model = withMastra(openai('gpt-4o'), {
  inputProcessors: [loggingProcessor],
});

const { text } = await generateText({
  model,
  prompt: 'What is 2 + 2?',
});
```

## Parameters

<PropertiesTable
  content={[
    {
      name: "model",
      type: "LanguageModelV2",
      description: "Any AI SDK language model (e.g., `openai('gpt-4o')`, `anthropic('claude-3-opus')`).",
      isOptional: false,
    },
    {
      name: "options",
      type: "WithMastraOptions",
      description: "Configuration object for processors and memory.",
      isOptional: true,
    },
    {
      name: "options.inputProcessors",
      type: "InputProcessor[]",
      description: "Input processors to run before the LLM call.",
      isOptional: true,
    },
    {
      name: "options.outputProcessors",
      type: "OutputProcessor[]",
      description: "Output processors to run on the LLM response.",
      isOptional: true,
    },
    {
      name: "options.memory",
      type: "WithMastraMemoryOptions",
      description: "Memory configuration - enables automatic message history persistence.",
      isOptional: true,
    },
    {
      name: "options.memory.storage",
      type: "MemoryStorage",
      description: "Storage adapter for message persistence (e.g., LibSQLStore, PostgresStore).",
      isOptional: false,
    },
    {
      name: "options.memory.threadId",
      type: "string",
      description: "Thread ID for conversation persistence.",
      isOptional: false,
    },
    {
      name: "options.memory.resourceId",
      type: "string",
      description: "Resource ID (user/session identifier).",
      isOptional: true,
    },
    {
      name: "options.memory.lastMessages",
      type: "number | false",
      description: "Number of recent messages to retrieve, or false to disable.",
      isOptional: true,
    },
    {
      name: "options.memory.semanticRecall",
      type: "WithMastraSemanticRecallOptions",
      description: "Semantic recall configuration (RAG-based memory retrieval).",
      isOptional: true,
    },
    {
      name: "options.memory.workingMemory",
      type: "MemoryConfig['workingMemory']",
      description: "Working memory configuration (persistent user data).",
      isOptional: true,
    },
    {
      name: "options.memory.readOnly",
      type: "boolean",
      description: "Read-only mode - prevents saving new messages.",
      isOptional: true,
    },
  ]}
/>

## Returns

A wrapped model compatible with `generateText`, `streamText`, `generateObject`, and `streamObject`.