Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@countly/ai-sdk-mastra

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@countly/ai-sdk-mastra

Countly AI observability adapter for Mastra

latest
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

@countly/ai-sdk-mastra

Countly AI observability adapter for Mastra.

Part of the Countly AI SDK — provider-agnostic LLM observability for every AI stack.

Install

npm install @countly/ai-sdk-mastra

@countly/ai-sdk-core is pulled in automatically.

Peer dependencies

@mastra/core >= 1.0.0
@mastra/observability >= 0.1.0

Quick Start

Note: the exporter must be wrapped in new Observability({...}) and passed via Mastra's observability: field. Passing it directly as exporters: on Mastra won't work — no spans reach the exporter.

import { Mastra } from "@mastra/core/mastra";
import { Observability } from "@mastra/observability";
import { CountlyMastraExporter } from "@countly/ai-sdk-mastra";

new Mastra({
  observability: new Observability({
    configs: {
      default: {
        serviceName: "my-ai-app",
        exporters: [
          new CountlyMastraExporter({
            appKey: "YOUR_APP_KEY",
            url: "https://your-countly-server.com",
            requestContextDeviceIdKey: "countlyDeviceId",
          }),
        ],
      },
    },
  }),
});

In your request handler:

import { RequestContext } from "@mastra/core/request-context";

app.post("/chat", async (req, res) => {
  const ctx = new RequestContext();
  ctx.set("countlyDeviceId", req.user.id);
  await mastra.getAgent("intent").stream(messages, { requestContext: ctx });
});

How the user ID reaches the event

The bridge is Mastra's runtime, not our SDK. We read a public field Mastra puts on every exported span:

Your handler                     Mastra runtime                  @countly/ai-sdk-mastra
─────────────                    ──────────────                  ──────────────────────
ctx = new RequestContext()
ctx.set("countlyDeviceId", id)
                                 run scope carries ctx
agent.stream(msg, {              ↓
  requestContext: ctx            spans created during run
})                               (AGENT_RUN, MODEL_GEN, TOOL_CALL)
                                 ↓
                                 ExportedSpan.requestContext = {
                                   countlyDeviceId: id              adapter reads
                                 }                                  span.requestContext
                                                                    ↓
                                                                    event.deviceId = id
                                                                    POST /i?device_id=id
  • Rename the key via requestContextDeviceIdKey (e.g. "myUserId")
  • Set to null to disable — falls back to getDeviceId() / deviceId / process UUID
  • No Mastra version requirement beyond >=1.0 — stable v1 observability surface

What's captured

  • Per-event tracing via exportTracingEvent (span_started, span_updated, span_ended)
  • Automatic trace completion when the root span ends (no parentSpanId)
  • Token usage, cost, and latency aggregated across all spans of a trace
  • Tool calls within agent workflows (function_call and mcp_tool_call types)
  • Error reporting for failed traces
  • Per-user aggregation across agent runs

Configuration

All adapters accept the same CountlyAIConfig object:

FieldDefaultDescription
appKeyrequiredCountly app key
urlrequiredCountly server URL
requestContextDeviceIdKey"countlyDeviceId"Key to read from Mastra's requestContext. Set to null to disable.
observabilityLevel00 = metrics only, 1 = + tool calls, 2 = + text previews
tags[]Labels for cost attribution and filtering
environment"production"Environment tag
costModelCustom pricing overrides
flushInterval10000Buffer flush interval in ms
maxBatchSize20Max events before auto-flush
debugfalseLog transport errors
disabledfalseDisable all telemetry

Full documentation

See the Countly AI SDK repository for the unified data model, observability levels, cost calculation, privacy controls, and Countly plugin integration (Drill, Funnels, Cohorts, APM, Crash Analytics).

License

MIT

FAQs

Package last updated on 12 May 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts