Application Architecture and Model Serving
Put the model inside a complete request path
A production request typically authenticates a user, validates input, retrieves authorized information, calls a model, validates output, and returns an answer or fallback. Each stage needs a timeout and an observable result.
flowchart LR A[Browser] --> B[Application API] B --> C[Authentication and limits] C --> D[Authorized retrieval or tools] D --> E[Model service] E --> F[Output validation] F --> A
Never put a privileged provider API key in browser code. A secret embedded in a frontend bundle is available to users. Browser-only local inference avoids that particular key problem but introduces device memory, download size, runtime support, and performance constraints.
Hosted, self-hosted, and local models
Hosted APIs reduce infrastructure work but introduce service dependencies, pricing, network latency, and contractual data-handling questions. Self-hosting gives more deployment control but requires model serving, capacity planning, security patches, and monitoring. On-device inference can support offline use but must account for varying hardware and model-download friction.
Choose using measured workload constraints. A deployment format that works on a laptop may not fit a low-memory mobile browser. Separate educational arithmetic labs from real LLM inference requirements.
Reliability controls
Use request IDs, explicit timeouts, cancellation, bounded retries, and backpressure. Retry transient transport or rate-limit failures under an appropriate policy; do not repeatedly retry invalid credentials or schema-invalid requests without changing anything.
If a user cancels a streamed answer, propagate cancellation to expensive downstream work where supported. A stream ending unexpectedly may leave an incomplete answer. Do not execute partial tool arguments before a full validated call has arrived.
Circuit breakers can temporarily stop calls to a failing dependency. Fallbacks should be honest: offer source excerpts or manual review rather than silently switching to an untested model with different permissions or behavior.
Checkpoint with answers
Can a public frontend safely hold a shared model-provider secret? No. Use a trusted server boundary or a suitable local model.
What should happen if retrieval times out? Follow the task policy: abstain, retry within budget, or return a limited fallback. Do not claim to have consulted evidence that was never retrieved.
Exercise: A streamed JSON response ends halfway through a refund amount. Should the app submit the amount? Answer: no. Wait for complete, validated structured output and the required authorization flow.