LLM Application Architecture: Core Patterns and How to Choose
Explore three core architecture patterns for LLM applications: general-purpose model orchestration, retrieval-augmented generation (RAG), and state-machine and graph orchestration. Learn what each pattern addresses, how its components work, and how to choose or combine them for enterprise AI applications.
As large language models (LLMs) advance, AI application development is moving beyond simple API calls toward more complex systems engineering. Developers building enterprise applications often face a difficult question: which architecture best fits their requirements?
The main architecture patterns are not competing replacements for one another. Each addresses a different set of engineering challenges. Understanding those distinctions is more useful than searching for a single “best” framework.
1. Three Core Architecture Patterns at a Glance
LLM application development can be viewed through three complementary architecture patterns:
- General-purpose model orchestration addresses how to call models, manage prompts, and enable tool use. It connects user input and system instructions with model capabilities and external tools.
- Retrieval-augmented generation (RAG) addresses how to load documents, build indexes, and retrieve relevant information. It focuses on data engineering to address knowledge gaps and reduce hallucinations, making it a key component of enterprise knowledge systems.
- State-machine and graph orchestration addresses how to manage state, handle loops, and recover from failures. It provides workflow control for applications involving multistep reasoning, iterative tasks, or collaboration among multiple agents.
These patterns operate at different layers and can be combined rather than treated as mutually exclusive choices.
2. General-Purpose Model Orchestration: Connecting Models, Prompts, and Tools
General-purpose model orchestration is often the first pattern developers encounter. Its central purpose is to connect an LLM with the application and the external systems it needs to use.
Model Calls and Prompt Management
LLM output quality depends heavily on the prompt. An orchestration layer provides standardized prompt templates that support variable substitution, few-shot examples, and the inclusion of conversational context.
Abstracting model calls also makes it easier to switch underlying models—for example, from an open-source model to a commercial model offered by a platform such as Baidu AI Cloud—without rewriting the surrounding business logic.
Tool Calling and Agent Behavior
An LLM cannot directly operate external systems on its own. Through function calling or the ReAct (Reasoning and Acting) pattern, an orchestration layer enables it to request tool execution.
Developers define tool descriptions and execution interfaces. During reasoning, the model can then determine when to request a tool and what arguments to provide.
# General-purpose tool definition example (pseudocode)@tooldef search_weather(location: str) -> str:"""Retrieve weather information for the specified city."""# Call the weather API and return the resultreturn weather_api.query(location)# Bind the tool to the orchestration chainchain = ModelChain(model=llm, tools=[search_weather])response = chain.run("What is the weather in Beijing today?")
This pattern is well suited to relatively linear workflows, conversational applications, and single-task execution.
3. Retrieval-Augmented Generation: Bringing Private Knowledge into Model Responses
When asked about enterprise data that is not available in its training knowledge, an LLM may produce unsupported answers. Retrieval-augmented generation addresses this problem by retrieving relevant information and providing it as context for generation.
Its core responsibilities are data ingestion, indexing, and retrieval.
Document Loading and Chunking
Enterprise knowledge often resides in unstructured formats such as PDF, Word, and HTML. A RAG pipeline first uses document parsers to extract text.
It then splits long documents into smaller, coherent chunks. Chunking strategy directly affects retrieval quality. Common approaches include:
- Fixed-length chunking
- Paragraph-based chunking
- Semantic chunking
The goal is to create text units that can be retrieved effectively while preserving useful context.
Embeddings and Index Construction
An embedding model converts text chunks into high-dimensional vectors. These vectors are stored and indexed in a vector database.
This indexing stage is central to RAG: it enables efficient retrieval across large document collections. The embedding and index design influence both retrieval quality and response time.
Retrieval and Reranking
When a user submits a question, the system embeds the question and searches the vector index for similar chunks, using a similarity measure such as cosine similarity.
More advanced RAG pipelines add a reranking stage. A cross-encoder scores the initial retrieval results again and reorders them, helping select the most relevant passages to include in the model’s context.
4. State-Machine and Graph Orchestration: Controlling Complex Workflows
As applications become more complex, simple linear chains can become difficult to use for loops, intermediate-state management, and failure recovery.
State-machine and graph orchestration provide explicit control over these workflows.
State Management and Transitions
This architecture represents an application as a directed graph:
- Nodes are execution units, such as model calls, tool execution, or data retrieval.
- Edges define the flow between those units.
A shared state object allows nodes to read and update contextual information. This supports state sharing across nodes and across successive iterations of a workflow.
Loops and Conditional Branches
Complex agent tasks often require repeated tool calls followed by evaluation of the results. This creates a reasoning–action–observation loop.
Graph orchestration supports conditional edges that select the next node based on the current state. These transitions make iterative workflows explicit.
# State-machine transition example (pseudocode)graph = StateGraph(State)def should_continue(state):# Determine whether the loop should continueif state['last_action'] == 'FINISH':return ENDreturn "tools_node"graph.add_node("agent_node", call_model)graph.add_node("tools_node", execute_tools)graph.add_edge(START, "agent_node")graph.add_conditional_edges("agent_node", should_continue)graph.add_edge("tools_node", "agent_node") # Create a loopapp = graph.compile()
Failure Recovery and Fault Tolerance
Model calls can time out, and tool execution can fail. State persistence and checkpoint-based recovery help graph-driven workflows handle these interruptions.
When supported and configured in the orchestration framework, these mechanisms allow a workflow to restore its state from a previous checkpoint and retry execution, improving reliability without restarting the entire process.
5. Choosing and Combining Architecture Patterns
Architecture selection should follow business requirements, not the popularity of a particular framework.
- For a simple chatbot or single-turn task executor, general-purpose model orchestration is often sufficient.
- For enterprise knowledge-base question answering, where responses must draw on private documents, RAG is the relevant architecture.
- For multistep approvals, multi-agent collaboration, or iterative tasks, such as automated code auditing or in-depth industry report generation, state-machine and graph orchestration provide the workflow control needed.
In practice, these patterns are often used together. An enterprise AI application may use a state graph as its top-level workflow, invoke RAG modules within specific nodes to retrieve knowledge, and use general-purpose orchestration to call models and coordinate tools.
Underlying model APIs and compute resources can come from cloud platforms such as Baidu AI Cloud, subject to the services available in the deployment environment. The architecture patterns themselves provide a way to organize those resources into reliable, efficient, and scalable applications.
There is no single architecture that solves every LLM application challenge. The practical approach is to understand each pattern’s purpose and boundaries, then combine them according to the needs of the application.
