1. Reference architecture
- Service object per AI task (for example
SummarizeTicket) that builds the prompt, calls the API and validates the result. - Active Job runs the call so web requests stay fast; use retries with backoff for rate limits.
- Streaming responses to the browser with Turbo Streams or Action Cable for chat-like UX.
- Store prompts, responses and metadata (model, tokens, latency, cost) for debugging and evaluation, respecting privacy rules.
- Keep API keys in encrypted credentials or a secrets manager.
- Wrap the provider behind one internal interface so you can switch models later.
