aibridge

package
v2.38.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: AGPL-3.0 Imports: 36 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

aibridge

aibridge provides an HTTP handler that intercepts AI client requests bound for upstream AI providers (Anthropic, OpenAI, Copilot). It records token usage, prompts, and tool invocations per user. Optionally supports centralized MCP tool injection with allowlist/denylist filtering.

The handler is mounted by a host process. Today that host is coderd, which mounts the handler at /api/v2/ai-gateway/<provider>/*. Running aibridge as a separate process is planned for the future.

Architecture

┌─────────────────┐     ┌───────────────────────────────────────────┐
│    AI Client    │     │                    aibridge               │
│  (Claude Code,  │────▶│  ┌─────────────────┐    ┌─────────────┐   │
│   Cursor, etc.) │     │  │  RequestBridge  │───▶│  Providers  │   │
└─────────────────┘     │  │  (http.Handler) │    │  (Anthropic │   │
                        │  └─────────────────┘    │   OpenAI)   │   │
                        │                         └──────┬──────┘   │
                        │                                │          │
                        │                                ▼          │    ┌─────────────┐
                        │  ┌─────────────────┐    ┌─────────────┐   │    │  Upstream   │
                        │  │    Recorder     │◀───│ Interceptor │─── ───▶│    API      │
                        │  │ (tokens, tools, │    │ (streaming/ │   │    │ (Anthropic  │
                        │  │  prompts)       │    │  blocking)  │   │    │   OpenAI)   │
                        │  └────────┬────────┘    └──────┬──────┘   │    └─────────────┘
                        │           │                    │          │
                        │           ▼             ┌──────▼──────┐   │
                        │  ┌ ─ ─ ─ ─ ─ ─ ─ ┐      │  MCP Proxy  │   │
                        │  │    Database   │      │   (tools)   │   │
                        │  └ ─ ─ ─ ─ ─ ─ ─ ┘      └─────────────┘   │
                        └───────────────────────────────────────────┘
Components
  • RequestBridge: The main http.Handler that routes requests to providers
  • Provider: Defines bridged routes (intercepted) and passthrough routes (proxied)
  • Interceptor: Handles request/response processing and streaming
  • Recorder: Interface for capturing usage data (tokens, prompts, tools)
  • MCP Proxy (optional): Connects to MCP servers to list tool, inject them into requests, and invoke them in an inner agentic loop

Request Flow

  1. Client sends request to /anthropic/v1/messages or /openai/v1/chat/completions
  2. Actor extraction: Request must have an actor in context (via AsActor()). The host is responsible for authenticating the caller before invoking the handler.
  3. Upstream call: Request forwarded to the AI provider
  4. Response relay: Response streamed/sent to client
  5. Recording: Token usage, prompts, and tool invocations recorded

With MCP enabled: Tools from configured MCP servers are centrally defined and injected into requests (prefixed bmcp_). Allowlist/denylist regex patterns control which tools are available. When the model selects an injected tool, the gateway invokes it in an inner agentic loop, and continues the conversation loop until complete.

Passthrough routes (/v1/models, /v1/messages/count_tokens) are reverse-proxied directly.

Observability

Prometheus Metrics

Create metrics with NewMetrics(prometheus.Registerer):

Metric Type Description
interceptions_total Counter Intercepted request count
interceptions_inflight Gauge Currently processing requests
interceptions_duration_seconds Histogram Request duration
passthrough_total Counter Non-intercepted requests forwarded to the upstream
prompts_total Counter User prompt count
tokens_total Counter Token usage (input, output, cache read/write, provider extras)
injected_tool_invocations_total Counter Injected MCP tool invocations performed by the handler
non_injected_tool_selections_total Counter Client-defined tool selections returned by the model
circuit_breaker_state Gauge Circuit breaker state per provider/endpoint (0=closed, 0.5=half, 1=open)
circuit_breaker_trips_total Counter Times the circuit breaker transitioned to open
circuit_breaker_rejects_total Counter Requests rejected due to an open circuit breaker
Recorder Interface

Implement Recorder to persist usage data to your database:

  • aibridge_interceptions - request metadata (provider, model, initiator, timestamps)
  • aibridge_token_usages - input/output and cache read/write token counts per response
  • aibridge_user_prompts - user prompts
  • aibridge_tool_usages - tool invocations (injected and client-defined)
  • aibridge_model_thoughts - model reasoning content (thinking, reasoning summaries, commentary)
type Recorder interface {
    RecordInterception(ctx context.Context, req *InterceptionRecord) error
    RecordInterceptionEnded(ctx context.Context, req *InterceptionRecordEnded) error
    RecordTokenUsage(ctx context.Context, req *TokenUsageRecord) error
    RecordPromptUsage(ctx context.Context, req *PromptUsageRecord) error
    RecordToolUsage(ctx context.Context, req *ToolUsageRecord) error
    RecordModelThought(ctx context.Context, req *ModelThoughtRecord) error
}

Supported Routes

Each provider instance is mounted under /api/v2/ai-gateway/<name>, where <name> is the provider's configured name. For example, with an Anthropic provider named my-anthropic, its /messages endpoint would be reachable at /api/v2/ai-gateway/my-anthropic/v1/messages.

If a name is not set, the route path defaults to the provider's type: anthropic, openai, or copilot. The table below uses the default names.

(/*) denotes a route that handles both the exact path and any subpaths. A trailing /* denotes subpaths only.

Provider Route Type
Anthropic /anthropic/v1/messages Bridged (intercepted)
Anthropic /anthropic/v1/messages/count_tokens Passthrough
Anthropic /anthropic/v1/models(/*) Passthrough
Anthropic /anthropic/api/event_logging/* Passthrough
OpenAI /openai/v1/chat/completions Bridged (intercepted)
OpenAI /openai/v1/responses Bridged (intercepted)
OpenAI /openai/v1/responses/* Passthrough
OpenAI /openai/v1/conversations(/*) Passthrough
OpenAI /openai/v1/models(/*) Passthrough
Copilot /copilot/chat/completions Bridged (intercepted)
Copilot /copilot/responses Bridged (intercepted)
Copilot /copilot/models(/*) Passthrough
Copilot /copilot/agents/* Passthrough
Copilot /copilot/mcp/* Passthrough
Copilot /copilot/.well-known/* Passthrough

Documentation

Index

Constants

View Source
const (
	ProviderAnthropic = config.ProviderAnthropic
	ProviderBedrock   = config.ProviderBedrock
	ProviderOpenAI    = config.ProviderOpenAI
	ProviderCopilot   = config.ProviderCopilot
)

Const + Type + function aliases for backwards compatibility.

View Source
const (
	SSEEventTypeMessage = "message"
	SSEEventTypeError   = "error"
	SSEEventTypePing    = "ping"
)

Variables

This section is empty.

Functions

func AsActor

func AsActor(ctx context.Context, actorID, email string, metadata recorder.Metadata) context.Context

func NewAnthropicProvider

func NewAnthropicProvider(ctx context.Context, cfg config.Anthropic, bedrockCfg *config.AWSBedrock, claudePlatformCfg *config.AWSClaudePlatform) (provider.Provider, error)

NewAnthropicProvider constructs the Anthropic provider. At most one of bedrockCfg and claudePlatformCfg may be non-nil; both nil means direct Anthropic API access.

func NewBedrockProvider

func NewBedrockProvider(ctx context.Context, cfg config.Anthropic, bedrockCfg config.AWSBedrock) (provider.Provider, error)

func NewCopilotProvider

func NewCopilotProvider(cfg config.Copilot) provider.Provider

func NewDisabledProviderStub added in v2.34.0

func NewDisabledProviderStub(name, providerType string) provider.Provider

NewDisabledProviderStub returns a Provider that reports Enabled() == false and has no-op implementations for all other methods. Use this instead of constructing a concrete provider for disabled rows so that adding a new provider type does not require updating a switch here.

func NewMetrics

func NewMetrics(reg prometheus.Registerer) *metrics.Metrics

func NewOpenAIProvider

func NewOpenAIProvider(cfg config.OpenAI) provider.Provider

Types

type AWSBedrockConfig

type AWSBedrockConfig = config.AWSBedrock

type AWSClaudePlatformConfig

type AWSClaudePlatformConfig = config.AWSClaudePlatform

type AnthropicConfig

type AnthropicConfig = config.Anthropic

type CopilotConfig

type CopilotConfig = config.Copilot

type ErrorType added in v2.36.0

type ErrorType = recorder.ErrorType

type InflightGate

type InflightGate struct {
	// contains filtered or unexported fields
}

InflightGate counts admitted requests and links their contexts to Close. It is safe for concurrent use. Must be created with NewInflightGate.

func NewInflightGate

func NewInflightGate(logger slog.Logger) *InflightGate

NewInflightGate returns a gate ready to admit requests.

func (*InflightGate) Admit

func (g *InflightGate) Admit() (release func(), ok bool)

Admit returns nil, false after shutdown begins. Otherwise, release must be called exactly once when the handler returns.

func (*InflightGate) Close

func (g *InflightGate) Close()

Close stops admission and cancels request contexts without waiting. It does not close network connections or force handlers to return.

func (*InflightGate) Middleware

func (g *InflightGate) Middleware(next http.Handler) http.Handler

Middleware admits requests and links their contexts to Close, or returns 503 after shutdown begins.

func (*InflightGate) RequestContext

func (g *InflightGate) RequestContext(parent context.Context) (context.Context, func())

RequestContext derives a context canceled by either the parent or forced shutdown. The caller must invoke cleanup when the handler returns to unregister the shutdown callback and cancel the derived context.

func (*InflightGate) Shutdown

func (g *InflightGate) Shutdown(ctx context.Context) error

Shutdown stops admission and waits for all admitted requests to finish. If ctx expires, it returns ctx.Err() without canceling request contexts.

type InterceptionRecord

type InterceptionRecord = recorder.InterceptionRecord

type InterceptionRecordEnded

type InterceptionRecordEnded = recorder.InterceptionRecordEnded

type Metadata

type Metadata = recorder.Metadata

type Metrics

type Metrics = metrics.Metrics

type ModelThoughtRecord

type ModelThoughtRecord = recorder.ModelThoughtRecord

type OpenAIConfig

type OpenAIConfig = config.OpenAI

type PromptUsageRecord

type PromptUsageRecord = recorder.PromptUsageRecord

type Provider

type Provider = provider.Provider

type Recorder

type Recorder = recorder.Recorder

func NewRecorder

func NewRecorder(logger slog.Logger, tracer trace.Tracer, apiKeyID string, structured bool, base Recorder, middleware ...recorder.Middleware) Recorder

NewRecorder creates a Recorder which logs each record and refuses malformed ones before handing it to base.

middleware is inserted below the logging and validating middleware, so that every record is logged and checked before any of it runs, and above the tracing middleware, which must stay immediately above the recorder its spans measure. Policy that drops records, such as recorder.WithoutRecords, belongs here: it keeps NewRecorder to its own concerns and leaves the choice to the caller.

type RequestBridge

type RequestBridge struct {
	// contains filtered or unexported fields
}

RequestBridge is an http.Handler which is capable of masquerading as AI providers' APIs; specifically, OpenAI's & Anthropic's at present. RequestBridge intercepts requests to - and responses from - these upstream services to provide a centralized governance layer.

RequestBridge has no concept of authentication or authorization. It does have a concept of identity, in the narrow sense that it expects an [actor] to be defined in the context, to record the initiator of each interception.

RequestBridge is safe for concurrent use.

func NewRequestBridge

func NewRequestBridge(ctx context.Context, providers []provider.Provider, rec recorder.Recorder, mcpProxy mcp.ServerProxier, logger slog.Logger, m *metrics.Metrics, tracer trace.Tracer, opts ...RequestBridgeOption) (*RequestBridge, error)

NewRequestBridge creates a new *RequestBridge and registers the HTTP routes defined by the given providers. Any routes which are requested but not registered will be reverse-proxied to the upstream service.

A recorder.Recorder is also required to record prompt, tool, and token use.

mcpProxy will be closed when the RequestBridge is closed.

Circuit breaker configuration is obtained from each provider's CircuitBreakerConfig() method. Providers returning nil will not have circuit breaker protection.

func (*RequestBridge) ServeHTTP

func (b *RequestBridge) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP exposes the internal http.Handler, which has all [Provider]s' routes registered. It also tracks inflight requests.

func (*RequestBridge) Shutdown

func (b *RequestBridge) Shutdown(ctx context.Context) error

Shutdown drains requests until ctx expires, then cancels remaining requests without waiting for their handlers to return. MCP cleanup is attempted even while canceled handlers are still running.

type RequestBridgeOption added in v2.35.0

type RequestBridgeOption func(*RequestBridge)

func WithClock added in v2.35.0

func WithClock(clock quartz.Clock) RequestBridgeOption

type SSEEvent

type SSEEvent struct {
	Type  string
	Data  string
	ID    string
	Retry int
}

type SSEParser

type SSEParser struct {
	// contains filtered or unexported fields
}

func NewSSEParser

func NewSSEParser() *SSEParser

func (*SSEParser) AllEvents

func (p *SSEParser) AllEvents() map[string][]SSEEvent

func (*SSEParser) EventsByType

func (p *SSEParser) EventsByType(eventType string) []SSEEvent

func (*SSEParser) MessageEvents

func (p *SSEParser) MessageEvents() []SSEEvent

func (*SSEParser) Parse

func (p *SSEParser) Parse(reader io.Reader) error

type TokenUsageRecord

type TokenUsageRecord = recorder.TokenUsageRecord

type ToolUsageRecord

type ToolUsageRecord = recorder.ToolUsageRecord

Directories

Path Synopsis
Package client identifies AI clients and request correlation metadata.
Package client identifies AI clients and request correlation metadata.
Package credential defines shared upstream authentication and metadata.
Package credential defines shared upstream authentication and metadata.
Package headers defines HTTP header names and helpers shared by AI Gateway request handlers.
Package headers defines HTTP header names and helpers shared by AI Gateway request handlers.
awssig
Package awssig holds the shared AWS SigV4 signing and Bedrock Mantle routing helpers used by both the anthropic-go and openai-go interceptors.
Package awssig holds the shared AWS SigV4 signing and Bedrock Mantle routing helpers used by both the anthropic-go and openai-go interceptors.
Package interceptionerror categorizes terminal AI Gateway interception errors.
Package interceptionerror categorizes terminal AI Gateway interception errors.
internal
Package mcpmock is a generated GoMock package.
Package mcpmock is a generated GoMock package.
Package routing provides shared provider routing for AI Gateway handlers.
Package routing provides shared provider routing for AI Gateway handlers.
x
proxy
Package proxy provides stateless AI Gateway request routing.
Package proxy provides stateless AI Gateway request routing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL