# Building AI Powered Web Applications
Adding AI to a web app today usually means calling a hosted model API, not training your own model — this dramatically lowers the barrier to entry for most product features.
## A Basic API Call
```javascript
const response = await fetch("https://api.example.com/v1/messages", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "model-name",
messages: [{ role: "user", content: userInput }],
}),
});
```
Most AI providers expose a simple HTTP API, making integration similar to calling any other third-party service.
## Never Expose API Keys Client-Side
AI API calls should go through your own backend, not directly from the browser — otherwise your API key (and your bill) is exposed to anyone who inspects the network tab.
## Handling Streaming Responses
Many AI APIs support streaming responses token-by-token, which makes the UI feel much faster than waiting for a complete response before displaying anything.
## Structured Outputs
For features like generating structured data (e.g. a list of suggested courses), prompting the model to return valid JSON and parsing it programmatically is more reliable than parsing free-form text.
## Handling Errors and Rate Limits
AI APIs can fail, time out, or hit rate limits — build in retries, fallback messaging, and clear loading states rather than assuming every call succeeds.
## Cost Awareness
Unlike most APIs, AI calls have a direct, variable cost per request. Caching repeated queries and setting reasonable input length limits prevents unexpected bills.
## Conclusion
Building AI-powered features is more about solid API integration practices — security, error handling, and cost control — than deep machine learning knowledge.
Back to Blogs
Building AI Powered Web Applications
A practical starting point for adding AI features to a web application — from calling an LLM API to handling responses responsibly.
21 Jul 2026
6 min read