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)
~/.ccs/cliproxy/gemini-{account}.json- Access + refresh tokens~/.ccs/cliproxy/sessions.json- Session metadata
Proactive Token Refresh
CCS checks token expiry before every request:- Accounts for network latency
- Prevents mid-request expiry
- Ensures smooth user experience
- Send refresh_token to provider
- Receive new access_token (expires in 1 hour)
- Receive new refresh_token (expires in 7 days)
- Update
{provider}-{account}.json - Continue with original request
Session Persistence
Session Files
Location:~/.ccs/cliproxy/sessions.json
Structure:
Reference Counting
Purpose: Track multiple active CCS sessions reusing the same CLIProxy proxy/session state How it works:- 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:referenceCountreaches 0- Session idle for >7 days
- Manual logout via
ccs codex --logout
- Revoke refresh token with provider
- Delete token file
- Remove from
sessions.json - Update
accounts.jsonregistry
File Structure
Token Files
Per-account token storage: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 Output:
tokens command to inspect current state: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:-
Network interruption during refresh
-
Proxy interference
-
Firewall blocking OAuth endpoints
-
Concurrent refresh attempts
- Multiple sessions trying to refresh simultaneously
- Session files locked by another process
Token Refresh Fails
Symptom:[X] Auth error: Failed to refresh token
Causes:
-
Refresh token expired (>7 days)
-
Provider revoked access
- User revoked app permissions
- Provider detected suspicious activity
-
Clock skew
Session File Corruption
Symptom:[X] Config error: Failed to parse sessions.json
Recovery steps:
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:Environment Variables
Security Best Practices
Protect Token Files
Rotate Tokens Regularly
Monitor Token Usage
Enable request logging to track token usage:~/.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
