> ## Documentation Index
> Fetch the complete documentation index at: https://docs.venice.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Mastra Integration

> Build TypeScript agents, tools, and workflows with Mastra using Venice's private, OpenAI-compatible models.

[Mastra](https://mastra.ai/) is a TypeScript framework for building AI agents, tools, and workflows. Connect it to Venice by configuring a custom OpenAI-compatible model endpoint.

## Prerequisites

* Node.js 22.18 or later
* A [Venice API key](/guides/getting-started/generating-api-key)

## Create a Mastra project

<CodeGroup>
  ```bash npm theme={"system"}
  npm create mastra@latest
  ```

  ```bash pnpm theme={"system"}
  pnpm create mastra
  ```

  ```bash yarn theme={"system"}
  yarn create mastra
  ```

  ```bash bun theme={"system"}
  bunx create-mastra
  ```
</CodeGroup>

Add your Venice API key to the generated project's `.env` file:

```bash theme={"system"}
VENICE_API_KEY=your-venice-api-key
```

<Warning>
  Keep `.env` out of source control. Do not expose your Venice API key in browser-side code.
</Warning>

## Create a Venice-powered agent

Create an agent and point its model configuration at the Venice API:

```typescript theme={"system"}
// src/mastra/agents/venice-agent.ts
import { Agent } from '@mastra/core/agent'

export const veniceAgent = new Agent({
  id: 'venice-agent',
  name: 'Venice Agent',
  instructions: `
    You are a concise, privacy-respecting assistant.
    Give accurate answers and say when you are uncertain.
  `,
  model: {
    id: 'venice/venice-uncensored',
    url: 'https://api.venice.ai/api/v1',
    apiKey: process.env.VENICE_API_KEY,
  },
})
```

The first segment of `id` is Mastra's routing label. Mastra sends the remaining model ID, `venice-uncensored`, to Venice.

<Note>
  Set `url` to the base API URL shown above. Do not append `/chat/completions`; Mastra adds that path.
</Note>

Register the agent in your Mastra instance:

```typescript theme={"system"}
// src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { veniceAgent } from './agents/venice-agent'

export const mastra = new Mastra({
  agents: { veniceAgent },
})
```

Start the development server and open the URL shown in your terminal to test the agent in Mastra Studio:

```bash theme={"system"}
npm run dev
```

## Generate a response

Retrieve the registered agent and call `generate()`:

```typescript theme={"system"}
import { mastra } from './src/mastra/index'

const agent = mastra.getAgentById('venice-agent')
const result = await agent.generate(
  'Explain why zero data retention matters in two sentences.',
)

console.log(result.text)
console.log(result.usage)
```

## Stream a response

Use `stream()` when you want to display output as it arrives:

```typescript theme={"system"}
const agent = mastra.getAgentById('venice-agent')
const result = await agent.stream(
  'Write a short poem about private AI inference.',
)

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

## Add tools

Mastra tools use Zod schemas to validate their inputs and outputs. Use a Venice model that supports function calling when attaching tools to an agent.

```typescript theme={"system"}
// src/mastra/tools/model-info.ts
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'

export const modelInfoTool = createTool({
  id: 'model-info',
  description: 'Returns a short description of a Venice model',
  inputSchema: z.object({
    model: z.string().describe('A Venice model ID'),
  }),
  outputSchema: z.object({
    description: z.string(),
  }),
  execute: async ({ model }) => {
    const descriptions: Record<string, string> = {
      'venice-uncensored': 'A fast, uncensored general-purpose model.',
      'zai-org-glm-5-1': 'A private model for reasoning and tool use.',
    }

    return {
      description: descriptions[model] ?? `No description found for ${model}.`,
    }
  },
})
```

Attach the tool to an agent:

```typescript theme={"system"}
// src/mastra/agents/model-guide-agent.ts
import { Agent } from '@mastra/core/agent'
import { modelInfoTool } from '../tools/model-info'

export const modelGuideAgent = new Agent({
  id: 'model-guide-agent',
  name: 'Venice Model Guide',
  instructions: 'Help users choose a Venice model. Use modelInfoTool when needed.',
  model: {
    id: 'venice/zai-org-glm-5-1',
    url: 'https://api.venice.ai/api/v1',
    apiKey: process.env.VENICE_API_KEY,
  },
  tools: { modelInfoTool },
})
```

## Generate structured output

Pass a Zod schema through `structuredOutput` to receive validated data:

```typescript theme={"system"}
import { z } from 'zod'

const result = await veniceAgent.generate(
  'Compare private and data-retaining AI inference.',
  {
    structuredOutput: {
      schema: z.object({
        summary: z.string(),
        privacyBenefits: z.array(z.string()),
        recommendation: z.string(),
      }),
    },
  },
)

console.log(result.object)
```

If the selected model does not support the API's structured-output mode, set `jsonPromptInjection: true` inside `structuredOutput` to have Mastra add the schema to the prompt instead.

## Change models

To use another Venice text model, keep the `venice/` routing prefix and replace the remaining model ID:

```typescript theme={"system"}
model: {
  id: 'venice/qwen3-5-9b',
  url: 'https://api.venice.ai/api/v1',
  apiKey: process.env.VENICE_API_KEY,
}
```

Browse [available Venice models](/models/overview) and check their capabilities before selecting one for tool calling, vision, or structured output.

## Troubleshooting

<AccordionGroup>
  <Accordion title="The API returns 401 Unauthorized">
    Confirm that `VENICE_API_KEY` is present in the environment where Mastra runs, then restart the development server after changing `.env`.
  </Accordion>

  <Accordion title="The API reports that the model does not exist">
    Use the exact model ID shown on the [models page](/models/overview). In Mastra, prefix it with `venice/`, for example `venice/venice-uncensored`.
  </Accordion>

  <Accordion title="Requests return 404 Not Found">
    Set the model URL to `https://api.venice.ai/api/v1`. Do not use the full `/chat/completions` endpoint.
  </Accordion>

  <Accordion title="The agent does not call its tool">
    Choose a model that supports function calling, describe when the agent should use the tool in its instructions, and define the tool with `createTool()`.
  </Accordion>
</AccordionGroup>

<CardGroup cols={2}>
  <Card title="Mastra Documentation" icon="book" href="https://mastra.ai/docs">
    Learn more about Mastra agents, tools, and workflows
  </Card>

  <Card title="Venice Models" icon="database" href="/models/overview">
    Browse models and supported capabilities
  </Card>
</CardGroup>
