Files
coder/docs/ai-coder/ai-bridge/monitoring.md
T
Danny Kopping 43a1af3cd6 feat: session list API (#23202)
<!--

If you have used AI to produce some or all of this PR, please ensure you have read our [AI Contribution guidelines](https://coder.com/docs/about/contributing/AI_CONTRIBUTING) before submitting.

-->

_Disclaimer:_ _initially_ _produced_ _by_ _Claude_ _Opus_ _4\.6,_ _heavily_ _modified_ _and_ _reviewed_ _by_ _me._

Closes https://github.com/coder/internal/issues/1360

Adds a new `/api/v2/aibridge/sessions` API which returns "sessions".

Sessions, as defined in the [RFC](https://www.notion.so/coderhq/AI-Bridge-Sessions-Threads-2ccd579be59280f28021d3baf7472fbe?source=copy_link), are a set of interceptions logically grouped by a session key issued by the client.  
The API design for this endpoint was done in [this doc](https://github.com/coder/internal/issues/1360).

If the client has not provided a session ID, we will revert to the thread root ID, and if that's not present we use the interception's own ID (i.e. a session of a single interception - which is effectively what we show currently in our `/api/v2/aibridge/interceptions` API).

The SQL query looks gnarly but it's relatively simple, and seems to perform well (~200ms) even when I import dogfood's `aibridge_*` tables into my workspace. If we need to improve performance on this later we can investigate materialized views, perhaps, but for now I don't think it's warranted.

---

_The PR looks large but it's got a lot of generated code; the actual changes aren't huge._
2026-03-24 08:58:47 +02:00

5.4 KiB

Monitoring

AI Bridge records the last user prompt, token usage, model reasoning, and every tool invocation for each intercepted request. Each capture is tied to a single "interception" that maps back to the authenticated Coder identity, making it easy to attribute spend and behaviour.

User Prompt logging

User Leaderboard

We provide an example Grafana dashboard that you can import as a starting point for your metrics. See the Grafana dashboard README.

These logs and metrics can be used to determine usage patterns, track costs, and evaluate tooling adoption.

Exporting Data

AI Bridge interception data can be exported for external analysis, compliance reporting, or integration with log aggregation systems.

REST API

You can retrieve AI Bridge sessions via the Coder API, with filtering and pagination support.

curl -X GET "https://coder.example.com/api/v2/aibridge/sessions" \
  -H "Coder-Session-Token: $CODER_SESSION_TOKEN"

Available query filters:

  • client - Filter by client name.

    Possible client values

    Note

    Client classification is done on best effort basis using the User-Agent header; not all clients send these headers in an easily-identifiable manner.

    • Claude Code
    • Codex
    • Zed
    • GitHub Copilot (VS Code)
    • GitHub Copilot (CLI)
    • Kilo Code
    • Mux
    • Roo Code
    • Cursor
    • Unknown

  • initiator - Filter by user ID or username

  • provider - Filter by AI provider (e.g., openai, anthropic)

  • model - Filter by model name

  • started_after - Filter interceptions after a timestamp

  • started_before - Filter interceptions before a timestamp

See the API documentation for full details.

CLI

Export interceptions as JSON using the CLI:

coder aibridge interceptions list --initiator me --limit 1000

You can filter by time range, provider, model, and user:

coder aibridge interceptions list \
  --started-after "2025-01-01T00:00:00Z" \
  --started-before "2025-02-01T00:00:00Z" \
  --provider anthropic

See coder aibridge interceptions list --help for all options.

Data Retention

AI Bridge data is retained for 60 days by default. Configure the retention period to balance storage costs with your organization's compliance and analysis needs.

For configuration options and details, see Data Retention in the AI Bridge setup guide.

Tracing

AI Bridge supports tracing via OpenTelemetry, providing visibility into request processing, upstream API calls, and MCP server interactions.

Enabling Tracing

AI Bridge tracing is enabled when tracing is enabled for the Coder server. To enable tracing set CODER_TRACE_ENABLE environment variable or --trace CLI flag:

export CODER_TRACE_ENABLE=true
coder server --trace

What is Traced

AI Bridge creates spans for the following operations:

Span Name Description
CachedBridgePool.Acquire Acquiring a request bridge instance from the pool
Intercept Top-level span for processing an intercepted request
Intercept.CreateInterceptor Creating the request interceptor
Intercept.ProcessRequest Processing the request through the bridge
Intercept.ProcessRequest.Upstream Forwarding the request to the upstream AI provider
Intercept.ProcessRequest.ToolCall Executing a tool call requested by the AI model
Intercept.RecordInterception Recording creating interception record
Intercept.RecordPromptUsage Recording prompt/message data
Intercept.RecordTokenUsage Recording token consumption
Intercept.RecordToolUsage Recording tool/function calls
Intercept.RecordInterceptionEnded Recording the interception as completed
ServerProxyManager.Init Initializing MCP server proxy connections
StreamableHTTPServerProxy.Init Setting up HTTP-based MCP server proxies
StreamableHTTPServerProxy.Init.fetchTools Fetching available tools from MCP servers

Example trace of an interception using Jaeger backend:

Trace of interception

Capturing Logs in Traces

Note

Enabling log capture may generate a large volume of trace events.

To include log messages as trace events, enable trace log capture by setting CODER_TRACE_LOGS environment variable or using --trace-logs flag:

export CODER_TRACE_ENABLE=true
export CODER_TRACE_LOGS=true
coder server --trace --trace-logs