Implements comprehensive BYOK (Bring Your Own Key) functionality, all… #329
+1,168
−72
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.
🔑 Add BYOK (Bring Your Own Key) Support for AI Provider APIs
Summary
Implements comprehensive BYOK (Bring Your Own Key) functionality, allowing SDK users to provide their own API keys for Anthropic, Gemini, and OpenAI models. Users can now use Codebuff's agent infrastructure while paying directly for LLM API costs through their own provider accounts, with reduced or zero Codebuff markup.
Motivation
require
mode, users can authenticate solely with provider keysChanges
🎨 Frontend (Web UI)
New Files:
web/src/app/api/user-api-keys/route.ts
- REST API for managing user keys (GET, POST)web/src/app/api/user-api-keys/[keyType]/route.ts
- REST API for deleting keys (DELETE)web/src/app/profile/components/user-api-keys-section.tsx
- Card-based UI for key managementModified Files:
web/src/app/profile/page.tsx
- Added "Provider API Keys" section to profile📦 SDK Layer
Modified Files:
sdk/src/client.ts
- AddeduserApiKeys
andbyokMode
options with authentication logicsdk/src/run.ts
- ExtendedCodebuffClientOptions
to accept BYOK parameterssdk/README.md
- Added comprehensive BYOK documentation and examplesNew Files:
sdk/knowledge.md
- Detailed architecture documentation for BYOK🔧 Backend
Modified Files:
backend/package.json
- Added@ai-sdk/[email protected]
dependencybackend/src/llm-apis/vercel-ai-sdk/ai-sdk.ts
- Implemented direct-to-provider routing with BYOK supportbackend/src/llm-apis/message-cost-tracker.ts
- Added per-provider cost tracking with reduced markupbackend/src/main-prompt.ts
- AddedgetUserApiKeys()
for DB key retrieval and mergingbackend/src/run-agent-step.ts
- Pass BYOK parameters through agent executionbackend/src/prompt-agent-stream.ts
- Pass BYOK parameters to AI SDK functions🔐 Common Layer
Modified Files:
common/src/actions.ts
- AddeduserApiKeys
andbyokMode
to CLIENT_ACTION_SCHEMAcommon/src/api-keys/crypto.ts
- AddedvalidateApiKey()
function for format validation📝 Documentation
New Files:
.plan/byok-implementation-summary.md
- Complete implementation summaryKey Features
1. Three BYOK Modes
2. Direct-to-Provider Routing
@ai-sdk/anthropic
(direct)@ai-sdk/google
(direct)@ai-sdk/openai
(direct)3. Key Precedence
4. Security
5. Cost Tracking
API Changes
SDK Constructor
Web API Endpoints
GET /api/user-api-keys
- List configured key typesPOST /api/user-api-keys
- Add/update API keyDELETE /api/user-api-keys/[keyType]
- Remove API keyDatabase Schema
Uses existing
encrypted_api_keys
table:Testing
Manual Testing Steps
/profile?tab=user-api-keys
byokMode
settingsRecommended Unit Tests
validateApiKey()
with valid/invalid keysgetUserApiKeys()
determineByokProvider()
logicRecommended Integration Tests
Breaking Changes
None. This is a fully backward-compatible addition. Existing code continues to work without any changes.
Migration Guide
No migration needed. To adopt BYOK:
/profile?tab=user-api-keys
userApiKeys
in constructor orrun()
optionsDocumentation
sdk/README.md
with usage examplessdk/knowledge.md
with architecture details.plan/byok-implementation-summary.md
with implementation summaryFuture Enhancements
Checklist