Best practices
These recommendations help you keep cost, speed and reliability under control when working with the codexkey API.
Choose the right model
Match the model to the task complexity:
| Task | Recommended model |
|---|---|
| Complex reasoning, research | claude-opus-5-5 |
| General coding, everyday tasks | claude-sonnet-5 |
| Quick completions, classification | gpt-6-luna |
For simple tasks a smaller model cuts both cost and latency noticeably.
Use prompt caching
When the same system prompt goes out with many requests, turn on caching: it makes input tokens cheaper.
Mark the system message block with the cache_control field:
{
"system": [
{
"type": "text",
"text": "You are a helpful coding assistant...",
"cache_control": { "type": "ephemeral" }
}
]
}Set appropriate max_tokens
Set max_tokens to the smallest value that works:
- Short answers: 256–512
- Code generation: 2048–4096
- Long-form content: 4096–8192
Lower values cut cost and sometimes reduce latency.
Use streaming for real-time responses
Turn streaming on: tokens arrive as they are generated, and the answer feels faster to the user:
curl https://api.codexkey.online/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: sk_cdx_YOUR_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-5",
"max_tokens": 1024,
"stream": true,
"messages": [
{"role": "user", "content": "Hello!"}
]
}'Use separate keys per environment
Create one key per environment, so usage does not mix and debugging gets easier:
- Production: a named key with a suitable channel and expiry
- Development: a separate key, so test spend is counted on its own
- CI/CD: a dedicated key for automated tests
That makes it simpler to watch spending, rotate keys and revoke access for each environment.
Monitor usage and set alerts
In the analytics dashboard, watch:
- Token consumption per key and model
- Cost trends over time
- Request counts by key
- Unexpected usage spikes
Turn on low-balance alerts in Settings so you hear about it before the funds run out.