Guide · OpenAI SDK
1. Stack
- Runtime: Next.js App Router (Route Handlers) or Edge Functions.
- SDK:
openaiofficial client +ai(Vercel AI SDK) for streaming. - Language: TypeScript with strict types.
2. Boilerplate
3. Patterns
- Function calling: define tool schema with Zod; handle
response.output[0].contentaccordingly. - Vision: send base64 images or remote URLs for document QA.
- Batching: use the Batch API for large eval jobs; monitor status webhooks.
4. Cost Controls
- Use
gpt-4o-minifor drafts,gpt-4ofor final outputs. - Log tokens per request; feed into cost observability dashboard.
- Cache responses (KV store) for deterministic prompts (e.g., onboarding instructions).
5. Safety
- Apply moderation endpoint before returning user-facing output.
- Combine with Guardrails (structured outputs + policy checks).
6. Testing
- Build fixtures using the Responses API
seedparameter for repeatability. - Run contract tests in CI hitting OpenAI mock server (e.g.,
msw).
sentience-sdk-starter (linked in repo) to get going fast.
Next up: Aussie →
