Large Language Models in Practice: A Scenario-Based Guide to Production Applications

    This guide explains how to build reliable large language model applications by starting from business scenarios rather than model size. It covers a four-layer capability model, prompt engineering as verifiable contracts, a standard long-document processing pipeline, RAG constraints for reducing hallucinations, controlled agent tool use, and cost, latency, and stability optimization, along with a staged learning path and common pitfalls.

    1. Start from the Scenario, Not the Model

    When many teams adopt large language models, their first instinct is to pick the model with the largest parameter count, only to find that the results fall well short of expectations. The root cause is that the model is only the capability foundation. Output quality is determined by scenario definition, context supply, and workflow orchestration. Practical learning should therefore start from everyday work scenarios, gradually unlock the right approach for different needs, and then work backward to the model capabilities required.

    A common mistake is treating “fluent conversation” as “usable in business.” Business usability requires at least three things: stable output structure, traceable facts, and a fallback for exceptions. None of these can be solved by swapping models alone; they all depend on engineering design.

    2. Capability Layers: Put the Model in the Right Place

    It helps to break large language model capabilities into four layers and validate them one by one:

    1. Base generation layer: summarization, rewriting, classification, and extraction. Evaluate format compliance and instruction sensitivity.
    2. Knowledge access layer: inject private enterprise knowledge into the context through retrieval to mitigate hallucinations and outdated information.
    3. Workflow orchestration layer: split multi-step tasks into nodes, connect them with a workflow engine, and support conditional branches and retries.
    4. Governance layer: log retention, access control, cost quotas, and content safety filtering.

    Only when all four layers are in place can an application move from “demo” to “production.”

    3. Prompt Engineering: From Vague Descriptions to Verifiable Contracts

    The core of a high-quality prompt is turning a natural-language request into a “verifiable contract” that specifies the input, output, constraints, and failure handling. A structured template is recommended:

    1. Role: You are a document structuring assistant
    2. Input: {{source_text}}
    3. Task: Extract fields and output JSON
    4. Constraints:
    5. - Use only information from the source text; fill missing fields with null
    6. - Do not add explanatory text
    7. - Normalize all timestamps to ISO 8601
    8. Failure handling: If the source text is insufficient, return {"error":"insufficient_context"}

    On top of this, few-shot examples can significantly improve format stability. Note that the examples should cover edge cases such as empty values, very long text, and mixed languages, rather than only ideal samples.

    4. Document Processing Pipeline: A Standard Approach for Long Text

    In enterprise scenarios, long-document question answering makes up a large share of usage. A practical pipeline typically has five stages:

    • Parsing: convert formatted files into plain text while preserving heading hierarchy and table structure;
    • Chunking: split along semantic boundaries, with configurable chunk size and overlap to avoid cutting sentences;
    • Vectorization: use a general-purpose embedding model to generate vectors and write them to a vector retrieval service;
    • Retrieval and reranking: do coarse recall first, then use a reranking model to improve relevance;
    • Generation and citation: require the model to output conclusions with source snippet IDs for easy human verification.

    A sample call flow (pseudocode) looks like this:

    1. chunks = split_by_semantics(parse(doc), size=800, overlap=120)
    2. vectors = embed(chunks)
    3. index.upsert(vectors)
    4. hits = rerank(model=reranker, query=q, candidates=index.search(embed(q), top_k=20))[:5]
    5. answer = llm.generate(prompt=build_prompt(q, hits), require_citation=True)

    Engineering also needs to handle incremental updates and deletions to avoid “ghost documents” in the knowledge base.

    5. Retrieval-Augmented Generation: Key Constraints for Suppressing Hallucinations

    Retrieval-augmented generation (RAG) does not mean “safe once retrieval happens.” To genuinely suppress hallucinations, three constraints are needed: first, a prompt-level constraint to “answer only based on the provided material”; second, a post-processing layer that performs consistency checks by comparing conclusions against cited snippets; third, a fallback strategy that returns “no supporting evidence found” when recall relevance falls below a threshold, instead of forcing generation.

    For evaluation, build a small labeled set covering at least four question types: factual, comparative, computational, and refusal, and measure accuracy and correct-refusal rate separately.

    6. Agents and Tool Calling: Controllable Automation

    When a task requires multi-step reasoning or calls to external systems, agent orchestration can be used. The key principle is “least privilege plus an explicit state machine”: each tool exposes only the necessary parameters, every call logs its inputs and outputs, and timeouts and retry limits are set to prevent loops from wasting resources.

    1. {
    2. "tool": "query_order",
    3. "args": {"order_id": "A123"},
    4. "timeout_ms": 3000,
    5. "max_retry": 2,
    6. "on_error": "ask_user"
    7. }

    7. Cost, Latency, and Stability

    Production environments need to optimize three metrics at once: reduce cost by caching frequent questions, compressing context length, and batching offline retrieval; reduce time to first token through streaming output and parallel retrieval; ensure availability with fallback paths (small-model fallback, rule-based fallback). It is recommended to connect quotas and alerts to a monitoring and alerting system, with thresholds for abnormal consumption.

    8. Learning Path and Common Pitfalls

    A four-stage path is recommended: first templatize single-turn tasks, then build knowledge base question answering, then introduce workflow orchestration, and finally complete the evaluation and governance systems. Common pitfalls include over-relying on very long context, lacking an evaluation set, ignoring data access boundaries, and treating demo results as the launch standard.

    9. Conclusion

    The practical capability of large language models is essentially a combination of scenario abstraction, context engineering, and process governance. Built on general cloud capabilities from Baidu AI Cloud (such as object storage, container platform, log service, and monitoring and alerting), teams can focus their effort on business logic and evaluation systems, crossing from prototype to production at a lower trial-and-error cost.