Software Engineering • AI Systems Engineering • 4 min read

AI Application Architecture

An AI model is not a complete product. Consider a document-question answering app: users need an interface, authentication, access to documents, reliable APIs, a model provider, logs, and safeguards. AI application architecture is how these parts fit together.

1. Start with the ordinary software system

A user sends a request through a website or mobile app. A backend validates the request, determines what the user is allowed to access, calls services, and returns a response. Model inference is one operation inside that larger process, not a replacement for the rest.

A helpful first design is:

Web/mobile UI
    |
Authenticated backend API
    |---- Relational database (users, metadata)
    |---- Retrieval service (documents and index)
    |---- Model gateway (hosted/local model)
    |---- Tool services (explicit allowed actions)
    |
Logs, tracing, evaluation and monitoring

2. Component responsibilities

ComponentResponsibilityKey concern
ClientInput, response rendering, user feedbackAccessibility and safe output display
BackendIdentity, business rules, rate limitsAuthorization and validation
Model gatewayModel selection, inference requests, fallbackCost, latency, provider errors
Retrieval layerSearch approved sourcesFreshness, permissions, provenance
Data storesPersist app data and task stateConsistency, retention, security
Background workerLong-running or retryable jobsIdempotency and observability
MonitoringMetrics, tracing, evaluationsPrivacy and operational reliability

3. Follow one request end to end

A student asks, “What documents do I need for enrollment?” The client sends the message and a session identifier. The backend authenticates the user, retrieves an authorized policy passage, builds the model input, and calls the model. The backend validates the generated output, attaches a source citation, logs operational metadata, and returns the answer.

Important: Document access permissions are checked by backend services, not left to the model to infer from a prompt.

4. Data flow and trust boundaries

User requests, retrieved pages, and tool responses are all untrusted inputs. A retrieved web page that says “ignore previous instructions” must be treated as document content, not a new authority. The application needs clear separation between instructions, evidence, and tool privileges.

Do not put provider API keys into mobile or browser code. Keep secrets in server-side configuration or secret management systems. Limit logged personal information and set explicit retention periods.

5. Synchronous versus asynchronous execution

A short response can be handled synchronously within a normal HTTP request. Large document indexing, batch inference, or expensive analysis may run as a background job: enqueue work, store job status, process it with a worker, and let the client poll or receive an authorized update.

Use streaming when progressive token display improves user experience; remember that streaming does not reduce the underlying computation automatically.

6. Reliability patterns

  • Apply request validation and timeouts at every service boundary.
  • Retry transient errors with bounded backoff; avoid blindly retrying non-idempotent actions.
  • Add rate limiting and budget enforcement for expensive model calls.
  • Record request IDs and structured traces without unnecessarily storing sensitive prompts.
  • Provide useful fallback behavior when a model or search index is unavailable.
  • Separate offline quality evaluation from production incident monitoring.

7. Example: a lightweight FastAPI boundary

This example demonstrates validation and service boundaries. The placeholder is intentionally not a real model call.

from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class Question(BaseModel):
    text: str = Field(min_length=1, max_length=2000)

@app.post('/ask')
def ask(question: Question):
    # Authenticate and authorize before retrieving private data.
    # Replace the placeholder with a bounded service call.
    return {'answer': f'Received: {question.text}', 'sources': []}

Production systems need authentication, error handling, safe logging, and a real model adapter; this example only illustrates the HTTP boundary.

8. Architecture decisions to document

Specify data ownership, model/provider dependencies, expected request volume, latency objectives, acceptable cost, privacy constraints, and failure behavior. Start with a small understandable architecture; split services only when scale or team boundaries justify the complexity.

References

Related articles in

  • Building Reliable AI Backends — Learn how validation, timeouts, retries, queues, security, and observability make AI APIs dependable.
  • What Is an API? — An API is a set of rules that lets one piece of software ask another piece of software for data or actions. Learn how APIs work, with clear examples.

Image preview

Fit