Building Agent Systems: Evolution and Engineering Practices for LLM Agent Architecture

    This article explains the core architecture of LLM agents, which combines a large language model with planning, memory, and tool calling. It covers how each module works, how to implement a basic tool-calling loop in Python, and the engineering practices needed for production systems, including sandboxed code execution, observability, rate limiting, and retries. It closes with the main open challenges for agent systems.

    As large language models (LLMs) continue to evolve, plain text generation can no longer meet the demands of complex business scenarios. Agent architectures, which use an LLM as the “brain” and add planning, memory, and tool-calling capabilities, are becoming the core direction for industries seeking to break through automation bottlenecks. This article analyzes the technical architecture and engineering practices of LLM agents and explores how to build highly available, scalable agent systems.

    1. Core Architecture of an Agent

    An agent is not simply prompt engineering. It is a complex system that can perceive its environment, make autonomous decisions, and carry out actions. Its core architecture typically includes four key modules: the brain (the LLM), planning, memory, and tool calling.

    1.1 The LLM as the Brain

    In an agent, the LLM serves as the central controller. It understands the user’s natural language intent, converts that intent into a structured task goal, and decides the next steps in execution. Choosing a suitable base model is the first step in building an agent. Developers need to select a model with strong logical reasoning and instruction-following abilities, based on task complexity, response latency requirements, and cost budget. For general business scenarios, cloud providers such as Baidu AI Cloud offer LLM inference services that provide stable, high-concurrency compute support.

    1.2 Planning and Reasoning

    When faced with a complex problem, an agent needs to break it down into executable subtasks. Common planning strategies in the industry include:

    • Chain of Thought (CoT): By prompting the model to “think step by step,” this strategy significantly improves accuracy in mathematical calculation and logical reasoning.
    • Tree of Thoughts (ToT): This approach lets the model explore multiple branches at each step, evaluate them, and backtrack when needed. It suits path-search problems.
    • ReAct pattern: This pattern combines reasoning with action. The model alternates between “thinking” and “calling tools.” It first considers what information it needs, calls a tool to get that information, and then uses the returned result to decide its next step. This cycle continues until it reaches a final answer.

    1.3 Memory Design

    Humans rely on memory to maintain coherent context, and agents need a memory system as well:

    • Short-term memory: This usually refers to the context window of the current conversation. Because model context length is limited, developers often use sliding windows or summarization to compress historical information.
    • Long-term memory: Historical interactions or external knowledge are converted into vectors and stored in a vector database. When a user asks a question, the agent first retrieves relevant past memories through similarity search and injects them into the current prompt. This enables personalization and knowledge accumulation across sessions.

    2. Tool Calling and Environment Interaction

    Tool calling is the bridge between an agent and the external world. By defining APIs or functions, an agent can query databases, execute code, and call third-party services.

    2.1 Function Calling Conventions

    Modern LLMs typically support function calling natively. Developers describe the names, parameters, and functions of available tools using JSON Schema. When the model determines that it needs external data during inference, it outputs a specific structured instruction instead of natural language text. The host program receives this instruction, executes the corresponding function, and returns the result to the model.

    2.2 Code Example: A Simple Tool-Calling Implementation

    The following is an abstract example in general-purpose Python. It shows how to define a tool and handle the model’s call requests:

    1. import json
    2. # Define the set of available tools
    3. available_tools = {
    4. "get_weather": {
    5. "description": "Get the current weather for a specified city",
    6. "parameters": {
    7. "type": "object",
    8. "properties": {
    9. "city": {"type": "string", "description": "City name, e.g., Beijing"}
    10. },
    11. "required": ["city"]
    12. }
    13. }
    14. }
    15. # Actual tool execution function
    16. def execute_get_weather(city: str) -> str:
    17. # Actual network request or database query omitted here
    18. return json.dumps({"city": city, "temperature": "25℃", "condition": "Sunny"})
    19. def agent_loop(user_query: str):
    20. # 1. Build the prompt, including tool descriptions
    21. system_prompt = f"You are a weather assistant. You can use the following tools: {json.dumps(available_tools, ensure_ascii=False)}"
    22. messages = [
    23. {"role": "system", "content": system_prompt},
    24. {"role": "user", "content": user_query}
    25. ]
    26. # 2. Call the LLM to get a decision (pseudocode here, representing interaction with the LLM service)
    27. # llm_response = call_llm_service(messages)
    28. llm_response = {
    29. "action": "get_weather",
    30. "args": {"city": "Beijing"}
    31. }
    32. # 3. Execute the tool and get the result
    33. if llm_response.get("action") == "get_weather":
    34. tool_result = execute_get_weather(**llm_response["args"])
    35. messages.append({"role": "tool", "name": "get_weather", "content": tool_result})
    36. # 4. Return the tool result to the model to generate the final natural-language reply
    37. # final_response = call_llm_service(messages)
    38. return f"Based on the query result, {tool_result}"
    39. print(agent_loop("What's the weather like in Beijing today?"))

    3. Enterprise Agent Engineering Practices

    Running agent logic in a lab environment is not difficult. In enterprise production environments, however, teams must address many engineering challenges, including high availability, security, and observability.

    3.1 Sandbox Isolation and Secure Execution

    When an agent needs to execute dynamically generated code, such as Python scripts, running that code directly on the host machine carries a serious security risk. Enterprise solutions typically use container technology to build isolated code execution sandboxes. For example, a cloud-native container platform can assign each code execution its own resource-limited container instance and destroy it immediately after execution. This prevents malicious code from damaging the system or leaking data.

    3.2 Logging, Monitoring, and Observability

    The decision-making process of an agent is a “black box.” When an agent produces an incorrect result, troubleshooting is very difficult. For this reason, teams must build a complete end-to-end logging system:

    • Decision logs: Record each step of the model’s reasoning, the tools it called, and their parameters.
    • Execution logs: Record tool execution time, returned results, and exception stack traces.

    By combining cloud logging services with monitoring and alerting systems, developers can track agent runtime status in real time, set alert thresholds for latency and error rate, and keep the system stable.

    3.3 Rate Limiting and Retry Mechanisms

    Calls to LLM APIs are vulnerable to network fluctuations and server-side rate limits. In an agent architecture, a distributed message queue should be introduced to smooth out traffic peaks. When a tool call fails or a model call times out, the system should retry using an exponential backoff strategy. In addition, when context overflow causes truncation errors, the agent should be able to trigger automatic compression of historical memory and retry, so it can recover on its own.

    4. Outlook and Challenges

    Agents have shown remarkable potential, but practical deployment still faces many challenges. For example, in multi-turn complex interactions, models can hallucinate or get stuck in loops. In multi-agent collaboration, communication overhead and state synchronization remain unsolved problems.

    As the reasoning capabilities of base models improve, and as infrastructure providers such as Baidu AI Cloud deepen their native support for agents, the barrier to entry for agent development is expected to drop significantly. Developers will need to keep track of the limits of model capabilities and keep their architectures flexible and robust. Only then can they build valuable intelligent applications during this wave of technological change.