# Monitor OpenCode Token Usage and Costs with Sentry

> Add Sentry AI observability to OpenCode in minutes. Track tool calls, token usage, model costs, and session durations across every coding session.

**URL:** https://sentry.io/cookbook/monitor-opencode-with-sentry/

---

Add the opencode-sentry-monitor plugin to get full visibility into tool calls, token usage, and model costs across every OpenCode session.

**Time:** 10 minutes | **Difficulty:** Beginner

## What You'll Learn

- Installed the opencode-sentry-monitor plugin into OpenCode

- Connected OpenCode sessions to Sentry AI Observability

- Tracked tool calls, LLM calls, token usage, and costs per session

- Explored per-session traces in Sentry's Explore > Agents

**Topics:** AI Observability, Agent Tracing, Tracing

**SDKs:** Node.js

## Before you start

**SDKs & packages**

- [OpenCode](https://opencode.ai/) installed and configured

- Node.js 18+ installed

**Accounts & access**

- [Sentry account](https://sentry.io/signup/) with a Node.js project created

**Knowledge**

- Basic familiarity with OpenCode configuration files

## Steps

### 1. Create a Sentry project

In Sentry, create a new project for your OpenCode monitoring data. Go to [Settings → Projects](https://sentry.io/orgredirect/organizations/:orgslug/settings/projects/) and click **Create Project**. Select **Node.js** as the platform, give it a name like `opencode`, and copy the DSN from the project settings — you'll need it in the next step.

[Creating a Sentry project](https://docs.sentry.io/product/sentry-basics/getting-started-tutorial/create-new-project/)

### 2. Install the plugin

The `opencode-sentry-monitor` package is an OpenCode plugin that automatically instruments your sessions. Add it to your OpenCode configuration file (`~/.config/opencode/opencode.json` or the project-level `opencode.json`).

```json
{
  "plugin": ["opencode-sentry-monitor"]
}
```

[opencode-sentry-monitor on npm](https://www.npmjs.com/package/opencode-sentry-monitor)

### 3. Configure your DSN and options

Create a config file at `.opencode/sentry-monitor.json` (or `~/.config/opencode/sentry-monitor.json` for a global setup). Add your DSN and set `tracesSampleRate` to `1` to capture everything during setup. You can optionally enable `recordInputs` and `recordOutputs` to capture tool inputs and outputs in your spans.

```json
{
  "dsn": "https://<your-dsn>@o<org>.ingest.sentry.io/<project-id>",
  "tracesSampleRate": 1,
  "recordInputs": true,
  "recordOutputs": true
}
```

### 4. Run OpenCode and generate activity

Start an OpenCode session by running `opencode` in your terminal and ask it to do a few things — read some files, run a command, write some code. The more tool calls it makes, the richer the data you'll see in Sentry. Every session will be captured as a `gen_ai.invoke_agent` span, with each tool execution — `bash`, `read`, `grep`, and others — tracked as a `gen_ai.execute_tool` child span. Token usage and model details are recorded automatically.

### 5. Explore tool calls and token usage in Sentry

Head to [Explore > Agents](https://sentry.io/orgredirect/organizations/:orgslug/explore/agents/) in Sentry. The **Traces** tab lists per-session traces, and the **LLM Calls** tab breaks down model requests by token usage and cost. Click any trace to drill into every tool call and message in that session.

[AI Observability documentation](https://docs.sentry.io/product/agents/)

## Summary

**Every tool call, tracked.**

You get a complete picture of how your AI coding agent uses models and tools — so you can tune it, debug it, and understand exactly what it's doing.

## Pro Tips

- You may want to adjust `tracesSampleRate` based on your traffic volume and how much from your sessions you want to send to Sentry.

- Use `recordInputs: false` and `recordOutputs: false` if your tool calls may contain secrets or sensitive file content.

- In Explore > Agents, filter by agent name to isolate OpenCode traces from other AI workloads in the same Sentry project.

- Environment variables `OPENCODE_SENTRY_DSN` and `OPENCODE_SENTRY_TRACES_SAMPLE_RATE` override the config file — useful for CI or shared machines.

## Common Pitfalls

- Forgetting to restart OpenCode after adding the plugin — it won't pick up the new config until the session is restarted.

- Using a Python or frontend Sentry DSN instead of a Node.js project — the plugin uses `@sentry/node` and needs a server-side project.

- Setting `tracesSampleRate: 0` by mistake — this silently disables all tracing, so no data appears in Sentry.

- Placing the config file in the wrong location — the plugin searches `.opencode/sentry-monitor.json` first, then `~/.config/opencode/sentry-monitor.json`.

## FAQ

**Does this work with all LLM providers in OpenCode?**

Yes. The plugin instruments OpenCode's session and tool layer, so it captures data regardless of which model provider (OpenAI, Anthropic, Gemini, etc.) you have configured in OpenCode.

**Will this slow down my coding sessions?**

No. Sentry uses asynchronous, non-blocking transport. Spans are batched and sent in the background with negligible performance impact.

**How much does it cost in Sentry?**

Traces are billed based on volume. With `tracesSampleRate: 1`, a heavy day of coding might generate a few hundred traces. Sentry's free tier includes 10,000 spans/month — more than enough to get started.

**Is my code or file content sent to Sentry?**

Only if you enable `recordInputs` and `recordOutputs` as shown in the config above. With those disabled, only span metadata — tool names, durations, token counts — is captured. No file contents or prompt text is sent.

**Can I use this in a team setting?**

Yes. Each developer configures the plugin with the same Sentry DSN to funnel all sessions into one project, giving you a combined view of AI agent usage across the team.

## Next Steps

- [Set up alerts on token spend](https://sentry.io/orgredirect/organizations/:orgslug/monitors/alerts/): Create a Sentry alert to notify you when token usage or cost spikes unexpectedly.

- [Explore full trace details](https://sentry.io/orgredirect/organizations/:orgslug/explore/traces/): Use the Trace Explorer to drill into individual OpenCode sessions and see every LLM call and tool invocation.

- [AI Observability docs](https://docs.sentry.io/product/agents/): Learn more about how Sentry tracks token usage, model costs, and tool calls for AI applications.

---

*Source: [sentry.io/cookbook/monitor-opencode-with-sentry/](https://sentry.io/cookbook/monitor-opencode-with-sentry/)*
