A Practical Guide to Building Production-Ready LLM Applications with Frameworks

    This article explains why getting a demo running is not the same as shipping a large language model application to production, and how LLM application frameworks help close that gap. It breaks down the six core capability layers—model I/O, retrieval, orchestration, agents, middleware, and memory/state—with practical guidance and code examples for each. It also covers the engineering implications of moving to a 1.0 framework release, a five-step adoption path, and a checklist of common mistakes to avoid. The goal is to show teams how to make LLM workflows orchestrable, outputs constrained, behavior controllable, and results measurable.

    1. The Starting Problem: Why “Using a Framework” and “Shipping to Production” Are Two Different Things

    Most teams build their first LLM application prototype fast. A few dozen lines of code are enough to assemble a prompt, call a model, and parse the result. Problems surface once the project moves past the demo stage: logic scattered across business code, unstable model output formats, tool calls without permission boundaries, inconsistent retry and timeout strategies, and missing logs and tracing that make issues impossible to diagnose.

    So the real question of “how should we use this” is not about how to call an API. It is about how to bring LLM capabilities into an engineering system that is maintainable, observable, and extensible.

    A common industry approach is to adopt an LLM application development framework. Its role is similar to a combination of an application foundation framework and an integration/orchestration component in the Java ecosystem: it exposes standardized development interfaces on top, abstracts away differences between model providers below, and uses orchestration in the middle to connect models, data, and external systems.

    2. Capability Layers: Understand the Components Before Using Them

    The first step to understanding a framework is to break down its component boundaries. The industry typically divides them into six parts. Each layer below explains what problem it solves and how to apply it in a project.

    1. Model Input/Output Layer (Model I/O)

    This layer contains three types of components:

    • Language model interfaces: A unified wrapper for chat models and text completion models that hides differences in authentication, timeouts, and retries.
    • Prompt templates: Inject variables into templates to decouple prompts from business code, making version management and multi-language adaptation easier.
    • Output parsers: Convert the natural language returned by a model into structured objects such as JSON, lists, or custom data classes.

    Practical points: Prompt templates should be under version control and support canary rollout. Output parsing must include a fallback for parse failures, for example one repair retry plus rule-based degradation.

    1. # Example: decoupling templates from structured parsing
    2. from framework import PromptTemplate, JsonParser
    3. tpl = PromptTemplate.from_template(
    4. "Extract fields from the ticket content, output JSON only:\n{content}"
    5. )
    6. parser = JsonParser(schema={"status": "string", "priority": "string"})
    7. prompt = tpl.format(content=raw_ticket)
    8. data = parser.parse(llm.invoke(prompt).content_blocks)

    2. Data Ingestion and Retrieval Layer (Retrieval)

    This includes document loading, splitting, vectorization and vector storage, and retrievers. The most common mistake in projects is “splitting documents without evaluating retrieval.” Recommendations:

    • Align splitting strategy with the structure of business documents (heading levels, tables, and code blocks should not be cut arbitrarily);
    • Build a retrieval evaluation set and focus on recall and context hit rate rather than demo quality alone;
    • Retrieval results should carry source identifiers so the frontend can show citations and support manual verification.

    3. Orchestration and Chain Layer (Chains / Graph)

    Early projects often used linear chains, but real business flows are usually “branches + loops + human confirmation.” Newer approaches have therefore generally shifted to state graph orchestration: nodes are defined as steps, edges as conditional transitions, and state as shared context. The benefits are visualizable flows, interruptible and resumable execution, and the ability to insert human approval nodes.

    4. Agent Layer

    Agent = model + tools + loop control. The value of a unified creation interface is that, whether the underlying implementation is a simple loop or state graph orchestration, the business side faces one consistent way to build agents.

    1. # Example: unified agent creation interface
    2. agent = create_agent(
    3. model="chat-model",
    4. tools=[query_order, create_ticket, search_kb],
    5. middleware=[auth_guard, trace_logger, rate_limiter],
    6. )
    7. result = agent.invoke({"messages": [{"role": "user", "content": "Help me check an order and create a ticket"}]})

    5. Middleware Layer

    Middleware is key to fine-grained flow control. Common uses include authentication and tool permission checks, sensitive data masking, call metering and rate limiting, unified logging and tracing, and failure retries and circuit breaking. Separating cross-cutting concerns from business prompts is the dividing line for code maintainability.

    6. Memory and State Layer (Memory / State)

    Three types of “memory” need to be distinguished: short-term conversation context, task-level intermediate state, and long-term user preferences. In production, it is not advisable to stuff all history into the context. Instead, use a combination of “summary compression + key entity extraction + external storage,” and integrate with common cloud capabilities such as logging services and object storage to achieve traceability and replayability.

    3. What Version Evolution Means for Engineering

    When a framework moves from early versions to a production-grade milestone like 1.0, four changes usually come with it. Understanding them helps avoid upgrade pitfalls:

    1. Unified interfaces: Agent creation methods converge, reducing the coexistence of multiple styles.
    2. Standardized output: Different models are unified into structured content blocks, so the business side does not need a branch for each model provider.
    3. Native structured output integration: Structured constraints move into the main loop, reducing the instability of “generate freely first, then parse.”
    4. Streamlined package structure: Redundant modules are moved into compatibility packages, the core becomes lighter, and the upgrade path requires explicit migration.

    Migration advice: First lock down the baseline behavior of the old version in a branch and build a regression test set. Then switch layer by layer in the order “model invocation layer → output parsing → orchestration layer → agent layer,” running the evaluation set and load tests after each layer switch.

    4. A Practical Path: Five Steps

    1. Scenario selection: Prioritize “high-frequency, low-risk, human-fallback” scenarios such as internal knowledge Q&A, ticket assistance, and report interpretation.
    2. Minimum closed loop: Get one chain working end to end—“input → retrieval → model → structured output → business write-back”—without pursuing multiple tools.
    3. Controllability: Add middleware to deliver the four essentials: permissions, rate limiting, masking, and tracing.
    4. Evaluation system: Build an offline evaluation set plus online sampling review, with metrics covering accuracy, format compliance rate, average latency, and cost per call.
    5. Progressive automation: Move gradually from “execute after human confirmation” to “automatic execution for low-risk actions,” while always keeping approval nodes for high-risk actions.

    5. Common Pitfalls and How to Avoid Them

    • Writing prompts as business logic: hard to test and reuse; templates and versioning are the answer.
    • Launching without evaluation: you cannot tell whether an upgrade or a model switch is a gain or a regression.
    • Overly broad tool permissions: when an agent can delete data or make payments, least privilege and secondary confirmation are mandatory.
    • Unbounded context growth: cost and noise rise together, so control both with summarization and retrieval.
    • Ignoring observability: without tracing, production issues are nearly impossible to locate.
    • One-shot big migration: roll out gradually by layer and keep a rollback switch.

    6. Conclusion

    The standard for whether a team truly “knows how to use” this kind of framework is not whether it can run the examples, but whether it has four capabilities: orchestrable flows, constrained outputs, controllable behavior, and measurable results. Once these four are embedded in team standards and code skeletons, swapping models, upgrading versions, and expanding scenarios become local changes rather than a complete rewrite. For a faster start, you can build the foundation with common capabilities such as the container platform, logging service, monitoring and alerting, and object storage offered by Baidu AI Cloud, and focus your effort on business orchestration and the evaluation system.