19. Best Practices#
- Keep keys server-side: do not expose API keys in frontend / mobile clients; route calls through your own backend proxy.
- Separate environments: create independent keys for production and testing, with sensible quotas, to isolate risk.
- Use streaming: enable
stream: truein user-facing chat scenarios to noticeably improve time to first token. - Control context length: trim history messages as needed in multi-turn conversations to save tokens and cost.
- Implement retries and timeouts: set reasonable timeouts for requests and retry retriable errors with backoff.
- Check models before calling: verify model names with
GET /v1/modelsearly in integration to avoid404. - Monitor usage: review console usage and billing regularly, and set quota alerts.