-
Notifications
You must be signed in to change notification settings - Fork 1.3k
chore: add README, CI integration, and lint fixes #24586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pawbana
merged 8 commits into
pb/aibridge-code-move-import-change
from
pb/aibridge-code-move-fixes
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a024b66
chore: Add updated README.md to aibridge
pawbana 408febb
add import restriction to aibridge directory
pawbana dcc85bc
chore: exclude typo linter for txtar files in aibridge/fixtures direc…
pawbana aa87a3a
chore: add aibridge directory to changes CI job
pawbana 21ce26d
chore: lint fixes: import order, go.mod and emdash
pawbana 4fb064a
chore: extend env vars to be unset in no credential test case of Test…
pawbana 5c4929c
review 1: import guard rename
pawbana a72693f
feat: exclude aibridge/fixtures directory from scripts/check_emdash.s…
pawbana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # aibridge | ||
|
|
||
| aibridge is an HTTP gateway that sits between AI clients and upstream AI providers (Anthropic, OpenAI). It intercepts requests to record token usage, prompts, and tool invocations per user. Optionally supports centralized [MCP](https://modelcontextprotocol.io/) tool injection with allowlist/denylist filtering. | ||
|
|
||
| ## 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()`). | ||
| 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 | | ||
| | `tokens_total` | Counter | Token usage (input/output) | | ||
| | `prompts_total` | Counter | User prompt count | | ||
| | `injected_tool_invocations_total` | Counter | MCP tool invocations | | ||
| | `passthrough_total` | Counter | Non-intercepted requests | | ||
|
|
||
| ### Recorder Interface | ||
|
|
||
| Implement `Recorder` to persist usage data to your database: | ||
|
|
||
| - `aibridge_interceptions` - request metadata (provider, model, initiator, timestamps) | ||
| - `aibridge_token_usages` - input/output token counts per response | ||
| - `aibridge_user_prompts` - user prompts | ||
| - `aibridge_tool_usages` - tool invocations (injected and client-defined) | ||
|
|
||
| ```go | ||
| 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 | ||
| } | ||
| ``` | ||
|
|
||
| ## Supported Routes | ||
|
|
||
| | Provider | Route | Type | | ||
| |----------|-------|------| | ||
| | Anthropic | `/anthropic/v1/messages` | Bridged (intercepted) | | ||
| | Anthropic | `/anthropic/v1/models` | Passthrough | | ||
| | Anthropic | `/anthropic/v1/messages/count_tokens` | Passthrough | | ||
| | OpenAI | `/openai/v1/chat/completions` | Bridged (intercepted) | | ||
| | OpenAI | `/openai/v1/models` | Passthrough | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,12 +257,19 @@ func TestAWSBedrockCredentialChain(t *testing.T) { | |
| SmallFastModel: "test-small-model", | ||
| }, | ||
| envVars: map[string]string{ | ||
| "AWS_ACCESS_KEY_ID": "", | ||
| "AWS_SECRET_ACCESS_KEY": "", | ||
| "AWS_SESSION_TOKEN": "", | ||
| "AWS_PROFILE": "", | ||
| "AWS_SHARED_CREDENTIALS_FILE": "/dev/null", | ||
| "AWS_CONFIG_FILE": "/dev/null", | ||
| "AWS_ACCESS_KEY_ID": "", | ||
| "AWS_SECRET_ACCESS_KEY": "", | ||
| "AWS_SESSION_TOKEN": "", | ||
| "AWS_PROFILE": "", | ||
| "AWS_SHARED_CREDENTIALS_FILE": "/dev/null", | ||
| "AWS_CONFIG_FILE": "/dev/null", | ||
| "AWS_WEB_IDENTITY_TOKEN_FILE": "", | ||
| "AWS_ROLE_ARN": "", | ||
| "AWS_ROLE_SESSION_NAME": "", | ||
| "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI": "", | ||
| "AWS_CONTAINER_CREDENTIALS_FULL_URI": "", | ||
| "AWS_CONTAINER_AUTHORIZATION_TOKEN": "", | ||
| "AWS_EC2_METADATA_DISABLED": "true", | ||
|
Comment on lines
+260
to
+272
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch 👍 |
||
| }, | ||
| expectError: true, | ||
| errorMsg: "no AWS credentials found", | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.