loading…
Search for a command to run...
loading…
Provides intelligent OpenAI API token management with automatic switching between model tiers when usage limits are reached. It enables users to track daily tok
Provides intelligent OpenAI API token management with automatic switching between model tiers when usage limits are reached. It enables users to track daily token consumption, estimate costs before making calls, and manage project-specific usage data.
A Model Context Protocol (MCP) server that provides intelligent OpenAI API token usage management with automatic model switching capabilities.
pip install -e .
Add this server to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"openai-token-manager": {
"command": "python",
"args": ["-m", "openai_token_manager_mcp.server"],
"env": {
"OPENAI_API_KEY": "your-openai-api-key-here"
}
}
}
}
# Run the MCP server
python -m openai_token_manager_mcp.server
initialize_token_managerInitialize the token manager with a specific project directory.
Parameters:
project_dir (string): Path to store token usage dataget_token_statusGet current token usage status and model information.
Returns: JSON with current model, usage statistics, and available models.
estimate_tokensEstimate token usage for given prompts before making API calls.
Parameters:
system_prompt (string): The system promptuser_prompt (string): The user prompt model (string, optional): Model to estimate forcall_openai_with_managementCall OpenAI API with automatic token management and model switching.
Parameters:
system_prompt (string): The system promptuser_prompt (string): The user promptresponse_format (string, optional): "json" for JSON response formattimeout (integer, optional): Request timeout in seconds (default: 45)force_model (string, optional): Force specific model (bypasses automatic switching)dry_run (boolean, optional): Simulate without making actual API callswitch_modelManually switch to the next available model tier.
reset_daily_usageReset daily token usage counters.
The default configuration includes:
MODEL_TIERS = [
{"name": "gpt-4o", "max_tokens": 250_000, "stop_at": 240_000},
{"name": "gpt-4o-mini", "max_tokens": 2_500_000, "stop_at": 2_450_000}
]
You can modify these in the server.py file to match your needs.
OPENAI_API_KEY: Your OpenAI API key (required)# Initialize for a specific project
await call_tool("initialize_token_manager", {"project_dir": "/path/to/project"})
# Check current status
status = await call_tool("get_token_status", {})
# Estimate tokens before calling
estimate = await call_tool("estimate_tokens", {
"system_prompt": "You are a helpful assistant.",
"user_prompt": "What is the weather like?"
})
# Make managed API call
response = await call_tool("call_openai_with_management", {
"system_prompt": "You are a helpful assistant.",
"user_prompt": "Explain quantum computing in simple terms.",
"response_format": "json"
})
# Force a specific model
response = await call_tool("call_openai_with_management", {
"system_prompt": "You are a helpful assistant.",
"user_prompt": "Write a short story.",
"force_model": "gpt-4o",
"timeout": 60
})
# Dry run to test without API calls
dry_response = await call_tool("call_openai_with_management", {
"system_prompt": "You are a helpful assistant.",
"user_prompt": "Analyze this data.",
"dry_run": True
})
# Manually switch models
await call_tool("switch_model", {})
# Reset usage for new day
await call_tool("reset_daily_usage", {})
When initialized, the token manager creates the following structure:
project_directory/
├── project_state/
│ └── token_usage.json # Token usage tracking
├── project_logs/
│ └── token_manager.log # Detailed logs
└── project_output/ # For any output files
The server includes comprehensive error handling:
Want to see a specific feature? Open an issue to request it!
MIT License - see LICENSE file for details
For issues and questions:
project_logs/token_manager.log for detailed error informationДобавь это в claude_desktop_config.json и перезапусти Claude Desktop.
{
"mcpServers": {
"openai-token-manager-mcp": {
"command": "npx",
"args": []
}
}
}