Skip to content

feat: Per-agent subscription token (self-service subscription management) #348

Open

Description

@vybe

Note: This feature is under consideration and depends on platform strategy decisions around self-service vs managed deployments. The design below reflects the current thinking but may evolve.

Summary

Allow users to set their own Claude subscription token at the user level, which applies to all agents they own. The behavior depends on how the user was onboarded:

  • Self-onboarded users → Must provide their own token (no pool fallback)
  • Admin-created users → Can use the managed subscription pool (current behavior)

This enables a mixed-mode deployment where internal teams use managed subscriptions while external/self-service users bring their own.

Current Behavior

Subscriptions are managed centrally via a shared pool:

  1. Admin registers subscription tokens in Settings → Subscriptions
  2. On agent creation, system auto-assigns a subscription via round-robin
  3. Rate-limited subscriptions trigger auto-switch to alternatives
  4. Agents fall back to platform API key if no subscription assigned

This works for admin-managed deployments but doesn't support self-service models where users manage their own subscriptions.

Proposed Behavior

Subscription behavior is determined by user onboarding path:

Self-Onboarded Users

  1. User signs up through self-service flow
  2. User runs claude setup-token to get their token
  3. User sets token via UI Settings or MCP tool
  4. All agents owned by this user use their token
  5. No fallback — if no token set, agents can't run

Admin-Created Users (unchanged)

  1. Admin creates user or whitelists their email
  2. Agents auto-assigned from subscription pool
  3. Rate-limit auto-switching works as before
  4. Platform manages subscription allocation

Technical Design

Database Changes

-- Add columns to users table
ALTER TABLE users ADD COLUMN subscription_token_encrypted TEXT;
ALTER TABLE users ADD COLUMN requires_own_subscription INTEGER DEFAULT 0;
-- 0 = can use pool (admin-created)
-- 1 = must provide own token (self-onboarded)

Auth Resolution Logic

def get_auth_for_agent(agent_name: str):
    owner = db.get_agent_owner(agent_name)
    user = db.get_user_by_id(owner["owner_id"])
    
    # 1. User's own token always takes priority
    user_token = db.get_user_subscription_token(user["id"])
    if user_token:
        return "user_subscription", user_token
    
    # 2. Pool fallback ONLY for admin-created users
    if not user["requires_own_subscription"]:
        subscription_id = db.get_agent_subscription_id(agent_name)
        if subscription_id:
            return "pool_subscription", db.get_subscription_token(subscription_id)
    
    # 3. Not configured
    return "not_configured", None

New DB Operations (db/users.py)

def set_user_subscription_token(user_id: int, token: str) -> None
def get_user_subscription_token(user_id: int) -> Optional[str]
def clear_user_subscription_token(user_id: int) -> None
def has_user_subscription_token(user_id: int) -> bool
def set_requires_own_subscription(user_id: int, required: bool) -> None

Container Lifecycle Changes

Modify lifecycle.py and crud.py to use the new auth resolution:

# Get auth for this agent based on owner's subscription mode
auth_mode, token = get_auth_for_agent(agent_name)

if token:
    env_vars['CLAUDE_CODE_OAUTH_TOKEN'] = token
    env_vars.pop('ANTHROPIC_API_KEY', None)
else:
    # No auth configured
    env_vars.pop('ANTHROPIC_API_KEY', None)
    env_vars.pop('CLAUDE_CODE_OAUTH_TOKEN', None)

New API Endpoints (routers/users.py)

Method Path Auth Description
PUT /api/users/me/subscription-token JWT Set subscription token
DELETE /api/users/me/subscription-token JWT Clear subscription token
GET /api/users/me/subscription-token/status JWT Check token status

New MCP Tool (mcp-server/src/tools/subscriptions.ts)

set_subscription_token: {
  name: "set_subscription_token",
  description: "Set your Claude subscription token. Get it by running `claude setup-token`.",
  parameters: z.object({
    token: z.string().describe("Token from `claude setup-token` (sk-ant-oat01-...)"),
  }),
}

UI Changes

Settings Page — "Your Subscription" section (visible to self-onboarded users):

  • Password input for token
  • Save/Clear buttons
  • Status indicator (configured / not configured)
  • Instructions: "Run claude setup-token to get your token"

Onboarding Flow Sets the Flag

Flow requires_own_subscription
Admin creates user via UI 0 (pool)
Email whitelist + login 0 (pool)
Self-signup flow (new) 1 (own token)

Running Agent Restart

When user sets/clears their token, restart all their running agents:

@router.put("/api/users/me/subscription-token")
async def set_subscription_token(body: TokenRequest, current_user: User):
    db.set_user_subscription_token(current_user.id, body.token)
    
    # Restart all running agents owned by this user
    owned_agents = db.get_agents_by_owner(current_user.username)
    for agent_name in owned_agents:
        container = get_agent_container(agent_name)
        if container and container.status == "running":
            await recreate_container_with_updated_config(...)

Files to Modify

File Changes
src/backend/db/schema.py Add columns to users table
src/backend/db/migrations.py Add migration
src/backend/db/users.py Add subscription token methods
src/backend/services/agent_service/lifecycle.py Use new auth resolution
src/backend/services/agent_service/crud.py Use new auth resolution
src/backend/services/subscription_service.py Add get_auth_for_agent()
src/backend/routers/users.py Add token endpoints
src/mcp-server/src/tools/subscriptions.ts Add set_subscription_token
src/mcp-server/src/client.ts Add client method
src/frontend/src/views/Settings.vue Add subscription section

Acceptance Criteria

  • Self-onboarded users can set their subscription token via UI
  • Self-onboarded users can set their subscription token via MCP tool
  • All agents owned by user use their token (if set)
  • Token is encrypted at rest (AES-256-GCM)
  • Running agents restart when token is set/cleared
  • Self-onboarded users have no pool fallback
  • Admin-created users retain current pool behavior
  • Auth mode API reflects actual source (user_subscription / pool_subscription / not_configured)

Open Questions

  1. Self-onboarding flow: What does the signup UX look like? Token required at signup or added later?
  2. Migration: How to handle existing users? Default to requires_own_subscription=0?
  3. Visibility: Should admin-created users also see the "set your own token" option (as override)?

Labels

  • enhancement
  • subscriptions
  • self-service

Activity

  1. added
    enhancement
    on Apr 15, 2026
  2. added
    complexity-medium
    theme-monetization
    on Jun 3, 2026
  3. added
    type-feature
    priority-p2
    on Jun 3, 2026
  4. added
    status-incubating
    on Jun 14, 2026
  5. removed
    enhancement
    on Aug 4, 2026
  6. added
    complexity-high
    and removed
    complexity-medium
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-highpriority-p2status-incubatingtheme-monetizationtype-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions