Chapter 19 of 211 min

19. Best Practices#

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