Mastra

Trace Mastra agents, workflows, and tools with Neatlogs (TypeScript).

Wrap the Mastra entities you build with wrapMastra — it patches Agent, Workflow, Vector, and Memory methods to emit method-level spans (AGENT, WORKFLOW, TOOL, LLM, RETRIEVER, VECTOR_STORE). Initialize Neatlogs once at startup, before constructing your entities.

Prerequisites

npm install neatlogs@latest @mastra/core @ai-sdk/openai

Usage

import { init, wrapMastra, flush, shutdown } from 'neatlogs';

async function main() {
  await init({ apiKey: process.env.NEATLOGS_API_KEY, workflowName: 'mastra-demo' });

  const { Agent } = await import('@mastra/core/agent');
  const { openai } = await import('@ai-sdk/openai');

  const agent = wrapMastra(
    new Agent({ name: 'assistant', instructions: 'Be concise.', model: openai('gpt-4o') }),
  );

  const res = await agent.generate('In one sentence, what is Mastra?');
  console.log(res.text);

  await flush();
  await shutdown();
}

main().catch(console.error);

Alternative: getMastraObservability(). If you prefer Mastra's native observability hook, pass await getMastraObservability() into new Mastra({ observability }). It's an async helper exported from the main neatlogs package and requires the extra @neatlogs/instrumentation-mastra package. For most apps, wrapMastra is simpler and needs no extra dependency.

On this page