Observability for Mastra with Opik

Mastra is the TypeScript agent framework designed to provide the essential primitives for building AI applications. It enables developers to create AI agents with memory and tool-calling capabilities, implement deterministic LLM workflows, and leverage RAG for knowledge integration.

Mastra’s primary advantage is its built-in telemetry support that automatically captures agent interactions, LLM calls, and workflow executions, making it easy to monitor and debug AI applications.

Mastra tracing

Getting started

Create a Mastra project

If you don’t have a Mastra project yet, you can create one using the Mastra CLI:

$npx create-mastra
$cd your-mastra-project

Install required packages

Install the necessary dependencies for Mastra observability:

$npm install @mastra/observability @mastra/otel

Add environment variables

Create or update your .env file with the following variables:

$# Your LLM API key
$OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
$
$# Opik configuration
$OPIK_API_KEY=<your-opik-api-key>
$OPIK_WORKSPACE_NAME=<your-workspace>
$OPIK_PROJECT_NAME=<your-project-name>

Set up an agent

Create an agent in your project. For example, create a file src/mastra/index.ts:

1import { Mastra } from "@mastra/core/mastra";
2import { Observability } from "@mastra/observability";
3import { OtelExporter } from "@mastra/otel";
4import { PinoLogger } from "@mastra/loggers";
5import { LibSQLStore } from "@mastra/libsql";
6import { Agent } from "@mastra/core/agent";
7import { openai } from "@ai-sdk/openai";
8
9const OPIK_API_KEY = process.env.OPIK_API_KEY!;
10const OPIK_WORKSPACE_NAME = process.env.OPIK_WORKSPACE_NAME!;
11const OPIK_PROJECT_NAME = process.env.OPIK_PROJECT_NAME!;
12
13export const chefAgent = new Agent({
14 name: "chef-agent",
15 instructions:
16 "You are Michel, a practical and experienced home chef " +
17 "You help people cook with whatever ingredients they have available.",
18 model: openai("gpt-4o-mini"),
19});
20
21export const mastra = new Mastra({
22 agents: { chefAgent },
23 storage: new LibSQLStore({
24 url: ":memory:",
25 }),
26 logger: new PinoLogger({
27 name: "Mastra",
28 level: "info",
29 }),
30 observability: new Observability({
31 configs: {
32 default: {
33 serviceName: "chef-agent",
34 exporters: [
35 new OtelExporter({
36 provider: {
37 custom: {
38 endpoint: "https://www.comet.com/opik/api/v1/private/otel/v1/traces",
39 protocol: "http/json",
40 headers: {
41 Authorization: OPIK_API_KEY,
42 "Comet-Workspace": OPIK_WORKSPACE_NAME,
43 projectName: OPIK_PROJECT_NAME,
44 },
45 },
46 },
47 }),
48 ],
49 },
50 },
51 }),
52});

Run Mastra development server

Start the Mastra development server:

$npm run dev

Head over to the developer playground with the provided URL and start chatting with your agent.

What gets traced

With this setup, your Mastra application will automatically trace:

  • Agent interactions: Complete conversation flows with agents
  • LLM calls: Model requests, responses, and token usage
  • Tool executions: Function calls and their results
  • Workflow steps: Individual steps in complex workflows
  • Memory operations: Context and memory updates

Validation

  1. Run the Mastra dev server and execute one agent chat.
  2. Confirm OTLP export requests are sent to your configured endpoint.
  3. Verify the trace in Opik under the expected workspace/project.

Source references

Further improvements

If you have any questions or suggestions for improving the Mastra integration, please open an issue on our GitHub repository.