Sentry AI Monitoring plugin for OpenCode.
This plugin captures OpenCode session lifecycle, tool execution spans, and assistant token usage into Sentry using AI Monitoring span conventions.
Before using this plugin, create (or reuse) a Sentry project configured for Node SDK ingestion.
- Project type:
JavaScript->Node.js - Why this type: OpenCode plugins run in a Node runtime, and this plugin uses
@sentry/node - Required: tracing enabled (
tracesSampleRate>0) so AI Monitoring spans are stored - DSN source: Project Settings -> Client Keys (DSN)
You can use an existing Node project if you already have one.
- Session-level
gen_ai.invoke_agentspans - Tool-level
gen_ai.execute_toolspans (inputs/outputs optional) - Assistant token usage spans via
message.updatedevents - Model request/response attributes on
gen_ai.requestandgen_ai.invoke_agentspans (gen_ai.request.messages,gen_ai.response.text) - Custom tags on all spans and error reports
- Unsampled metrics for token usage, response timing, and tool executions
- Sidecar config file support (no hardcoded DSN required)
- JSON and JSONC config support
- Redaction and truncation for large/sensitive payload attributes
- Add plugin package to OpenCode config:
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-sentry-monitor"]
}- Create a plugin config file with your DSN:
{
"dsn": "https://<public-key>@o<org>.ingest.sentry.io/<project-id>",
"tracesSampleRate": 1,
"recordInputs": true,
"recordOutputs": true
}- Save that file as one of:
.opencode/sentry-monitor.json.opencode/sentry-monitor.jsonc~/.config/opencode/sentry-monitor.json~/.config/opencode/sentry-monitor.jsonc
Restart OpenCode after installation.
The plugin looks for config in this order:
OPENCODE_SENTRY_CONFIG(explicit file path)- Project
.opencode/ OPENCODE_CONFIG_DIR- Directory of
OPENCODE_CONFIG - Platform defaults:
~/.config/opencode~/Library/Application Support/opencode~/AppData/Roaming/opencode
If no config file exists, environment overrides are still supported:
OPENCODE_SENTRY_DSN(orSENTRY_DSN)OPENCODE_SENTRY_TRACES_SAMPLE_RATEOPENCODE_SENTRY_RECORD_INPUTSOPENCODE_SENTRY_RECORD_OUTPUTSOPENCODE_SENTRY_MAX_ATTRIBUTE_LENGTHOPENCODE_SENTRY_DIAGNOSTICSOPENCODE_SENTRY_FLUSH_TIMEOUT_MSOPENCODE_SENTRY_DEBUGOPENCODE_SENTRY_ENABLE_METRICSOPENCODE_SENTRY_TAGS(format:key:value,key:value)SENTRY_ENVIRONMENTSENTRY_RELEASE
type PluginConfig = {
dsn: string;
tracesSampleRate?: number; // 0..1, default 1
environment?: string;
release?: string;
debug?: boolean;
diagnostics?: boolean; // default false
flushTimeoutMs?: number; // default 5000, 1000..60000
agentName?: string;
projectName?: string;
recordInputs?: boolean; // default true (tool input + model request messages)
recordOutputs?: boolean; // default true (tool output + model response text)
maxAttributeLength?: number; // default 12000
includeMessageUsageSpans?: boolean; // default true
includeSessionEvents?: boolean; // default true
enableMetrics?: boolean; // default false
tags?: Record<string, string>; // custom tags on all spans/metrics
};When enableMetrics: true, the plugin emits Sentry metrics (unsampled, 100% accurate) for usage attribution:
| Metric | Type | Unit | Emitted |
|---|---|---|---|
gen_ai.client.token.usage |
distribution | token | Per assistant message, tagged by token type (input/output/reasoning/cached_input) |
gen_ai.client.response.duration |
distribution | millisecond | Per assistant message response time |
gen_ai.client.tool.execution |
counter | — | Per tool execution, tagged with status (ok/error) |
All metrics include gen_ai.agent.name, opencode.project.name, gen_ai.request.model, opencode.model.provider, plus any custom tags.
Example config for team attribution:
{
"dsn": "https://...",
"enableMetrics": true,
"agentName": "my-agent",
"tags": {
"team": "platform",
"developer": "sergiy"
}
}npm install
npm run typecheck
npm run buildnpm publish- DSN is not a secret, but this plugin does not require hardcoding it.
- If
recordInputs/recordOutputsare enabled, payloads are redacted and truncated before being attached as span attributes. - AI spans are flushed on
session.idleandsession.deleted.
MIT