Skip to main content

Overview

CCS implements sophisticated token management to provide seamless authentication:
  • Proactive Refresh - Tokens renewed 5min before expiry (no interruptions)
  • Session Persistence - Reference counting tracks active sessions
  • Automatic Recovery - Handles network errors, token expiry gracefully
  • Multi-Account Support - Separate tokens per account

Token Lifecycle

OAuth Flow (Initial Authentication)

Files Created:
  • ~/.ccs/cliproxy/gemini-{account}.json - Access + refresh tokens
  • ~/.ccs/cliproxy/sessions.json - Session metadata

Proactive Token Refresh

CCS checks token expiry before every request:
Why 5 minutes?
  • Accounts for network latency
  • Prevents mid-request expiry
  • Ensures smooth user experience
Refresh Process:
  1. Send refresh_token to provider
  2. Receive new access_token (expires in 1 hour)
  3. Receive new refresh_token (expires in 7 days)
  4. Update {provider}-{account}.json
  5. Continue with original request

Session Persistence

Session Files

Location: ~/.ccs/cliproxy/sessions.json Structure:
Key Fields:

Reference Counting

Purpose: Track multiple active CCS sessions reusing the same CLIProxy proxy/session state How it works:
Why it matters:
  • Prevents premature token cleanup
  • Supports shared proxy sessions without tearing down token state too early
  • Tracks active usage
This is not the same thing as full per-session isolation. Multiple sessions can reuse the same CLIProxy session/proxy state while still sharing provider runtime state.

Session Cleanup

Automatic cleanup when:
  • referenceCount reaches 0
  • Session idle for >7 days
  • Manual logout via ccs codex --logout
Cleanup process:
  1. Revoke refresh token with provider
  2. Delete token file
  3. Remove from sessions.json
  4. Update accounts.json registry

File Structure

Token Files

Per-account token storage:
Token file format:

Account Registry

Location: ~/.ccs/cliproxy/accounts.json Purpose: Map nicknames to email addresses

Config Reference

Location: ~/.ccs/config.yaml Relevant sections:

Manual Token Operations

1

View Token Status

Use the tokens command to inspect current state:
Output:
2

Force Token Refresh

Manually trigger refresh without waiting for threshold:
Use cases:
  • Debugging refresh issues
  • Pre-warming tokens before long task
  • Recovering from network errors
3

Clear Expired Tokens

Remove tokens that can’t be refreshed:
This removes:
  • Tokens expired >7 days (refresh token expired)
  • Tokens with persistent errors
  • Orphaned session entries
4

Export Tokens (Advanced)

For migration or backup:
Security warning: Exported files contain sensitive tokens. Encrypt before storage:
5

Import Tokens (Advanced)

Restore from backup or migrate between machines:
Note: Only works for same provider. Cross-provider imports not supported.

Troubleshooting

UND_ERR_SOCKET Error

Full error:
Common causes:
  1. Network interruption during refresh
  2. Proxy interference
  3. Firewall blocking OAuth endpoints
  4. Concurrent refresh attempts
    • Multiple sessions trying to refresh simultaneously
    • Session files locked by another process
    Solution:

Token Refresh Fails

Symptom: [X] Auth error: Failed to refresh token Causes:
  1. Refresh token expired (>7 days)
  2. Provider revoked access
    • User revoked app permissions
    • Provider detected suspicious activity
    Solution:
  3. Clock skew

Session File Corruption

Symptom: [X] Config error: Failed to parse sessions.json Recovery steps:
Prevention: Enable debug logging to catch issues early:

Multiple Accounts Conflict

Symptom: Wrong account used despite --use flag Cause: Default account set in config overrides flag Solution:

Token Not Found After Re-Auth

Symptom: [X] Profile error: Account 'xyz' not found Cause: Account registry out of sync with token files Solution:

Advanced Patterns

Pre-Warming Tokens

For long-running tasks, refresh tokens before starting:

Monitoring Token Health

Create a cron job to check token status:

Shared Team Tokens

For teams using Remote Proxy: On server:
Clients automatically inherit server’s tokens - no local auth needed.

Environment Variables

Security Best Practices

Protect Token Files

Rotate Tokens Regularly

Recommended: Rotate every 30 days, or immediately if compromise suspected.

Monitor Token Usage

Enable request logging to track token usage:
Logs location: ~/.ccs/logs/cliproxy-requests.log

Next Steps

Multi-Account Setup

Configure and switch between multiple accounts

Headless CI/CD

Use persistent sessions in automation workflows