Build TypeScript agents, tools, and workflows with Mastra using Venice’s private, OpenAI-compatible models.
Mastra is a TypeScript framework for building AI agents, tools, and workflows. Connect it to Venice by configuring a custom OpenAI-compatible model endpoint.
Create an agent and point its model configuration at the Venice API:
// src/mastra/agents/venice-agent.tsimport { 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.
Set url to the base API URL shown above. Do not append /chat/completions; Mastra adds that path.
Register the agent in your Mastra instance:
// src/mastra/index.tsimport { 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:
Retrieve the registered agent and call generate():
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)
Use stream() when you want to display output as it arrives:
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)}
Pass a Zod schema through structuredOutput to receive validated data:
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.