- Messages APIs and streaming
- The major providers all use a list of role-based messages and return server-sent events when streaming is on. Learn the event types (text deltas, tool calls, usage, stop reason) so you handle streams that are cut off mid-way or stop because they hit max tokens.
- Structured outputs
- Use JSON Schema, often generated from Zod, so the provider enforces the shape through constrained decoding instead of just asking in the prompt. Still validate on the server, because the model can refuse, get truncated by max tokens, or return values that have the right type but the wrong meaning.
- Tool calling
- The model does not run tools itself: it returns a tool name and arguments, your code executes it and sends the result back in the next message. Understand parallel tool calls, how to return tool errors to the model as observations, and that tool definitions cost tokens on every request.
- Prompt caching
- Put the stable parts (system prompt, documents, tool definitions) first and the changing parts last so the prefix can be cached, which cuts input cost and latency significantly. Some providers cache automatically and others need explicit breakpoints, so check the cached token fields in the usage data to see whether the cache actually hits.
- Cost and latency budgets
- Output tokens are usually more expensive and slower than input, so capping max output is the most direct saving. Use batch APIs for offline jobs to get discounted pricing, set timeouts, retry with exponential backoff on 429 and overload errors, and always track cost per request and per user.
- Choosing models: hosted, smaller tiers or open-weight
- Hosted frontier models give the best quality, smaller tiers from the same vendor are much cheaper and faster for narrow tasks, and open-weight models give you control over data and cost at scale. A provider abstraction such as the AI SDK or an AI gateway lets you swap models without rewriting code, and the choice should rest on your own evals.