HAVE AI NEWS HAVE AI NEWS
tg subscribe
Tools

How an AI Agent Works: The Anatomy of a System from Token Prediction to MCP

How an AI Agent Works: The Anatomy of a System from Token Prediction to MCP

We examine the inner workings of autonomous neural network agents: how a standard text generator gains hands, memory, and access to real-world tools, why context breaks down, and where the primary risks of automation lie.

Any modern AI agent is not a fundamentally new form of artificial intelligence, but an engineering layer built on top of a standard language model. On its own, an LLM is capable of solving only one task: calculating probabilities and appending the next token based on context. To transform a passive text generator into an autonomous assistant capable of debugging code and managing services, developers must build a multi-layered infrastructure.

The Foundation: Why a Pure LLM Cannot Act

A base language model is restricted by three fundamental barriers:

  • Knowledge cutoff: The model is aware only of what was included in the training dataset at the time of training.
  • Illusion of competence: The neural network possesses no self-awareness regarding the boundaries of its own knowledge. When lacking data, it generates plausible-sounding fabrications with the same degree of confidence as accurate facts.
  • Lack of execution mechanisms: The model outputs exclusively sequences of characters and has no direct access to the file system or the network.

Attempting to solve these problems head-on—by loading an entire codebase or corporate documentation directly into the prompt—runs into hardware limitations. The context window demands massive hardware resources: memory consumption grows linearly, while the computational complexity of the attention mechanism scales quadratically during token processing. The only viable solution is teaching the model to invoke external tools on demand.

Tool Calling: Turning Text into Commands

To interact with external APIs, the concept of tool calling is used. The model does not execute code on its own. Instead, descriptions of available functions are passed into the system context as strict JSON schemas.

{
  "name": "get_weather",
  "description": "Returns the current weather in the specified city",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "The name of the city, for example, Moscow"
      }
    },
    "required": ["city"]
  }
}

Instead of arbitrary text, the neural network returns structured JSON containing arguments, guided by the textual description in the description field. The tool selection mechanism remains the same prediction of the most probable next token. If a function's description is written carelessly or overlaps with others, the model will inevitably start calling the wrong commands.

Agent Loop: The Feedback Cycle

Solving a complex problem (such as fixing broken automated tests) requires sequential steps where each subsequent action relies on the output of the previous one. This is where the agent loop comes in—a control cycle that, in its simplest form, is implemented as a standard while loop.

The lifecycle of a session is built on exchanging messages across four base roles:

  1. System: Global directives, safety constraints, and the list of available tools.
  2. User: The problem statement provided by a human.
  3. Assistant: The model's response (either text or a tool call request with a call_id).
  4. Tool: The actual execution output of the script or API request, returned back into the context.

With every turn of the loop, history accumulates and is re-sent to the model. This creates the illusion of real-time learning: step by step, the agent deepens its understanding of the project. However, the model weights remain completely unchanged, while the context window rapidly fills with operational output.

RAG and MCP: Standardizing External Data

To provide an agent with up-to-date facts about private repositories or issue tracking tickets, agentic RAG is utilized. The model decides on its own when to run a document search or search across project files, receiving the result via the Tool role.

To prevent fragmented, redundant integrations, the industry converged on the Model Context Protocol (MCP). It is an open client-server protocol: a third-party service runs an MCP server, and the agent's runtime environment connects to its tools without requiring modifications to the engine. However, this architecture has a downside: schemas for all connected servers must permanently reside in the system prompt. Connecting several heavy MCP servers can consume tens of thousands of tokens before the first user command is even processed.

Skills: Dynamic Context Optimization

The Skills mechanism optimizes memory utilization. Instead of storing extensive step-by-step scenarios in context, the agent receives only a concise table of contents. Detailed markdown instructions are loaded dynamically only when the model identifies that the task matches a specific scenario.

Skills strictly define business logic: which steps to follow, which terminal scripts to run, and the expected output format. This reduces the risk of the agent conflating disparate tasks or skipping critical checks.

Architectural Vulnerabilities and the Operator's Role

Any agentic architecture combines inherently unreliable components: a hallucinating language model, external APIs subject to network timeouts, and an OS terminal. Errors across these layers do not cancel each other out; they compound.

Important: Modern LLMs are fundamentally incapable of reliably distinguishing user instructions from input data. Text inside a tracker ticket or a fetched web page can contain an indirect prompt injection that overrides the agent's behavior.

Safe operation of agents requires strict adherence to security principles:

  • Permission granularity: Read operations are executed automatically, but any destructive actions (file modifications, state-changing API calls, shell execution) require mandatory human confirmation.
  • Isolation: An agent must never be simultaneously granted access to sensitive environment secrets and outbound communication channels.
  • Mandatory auditing: Code and configuration files must be committed to version control (Git) prior to running the agent, and final results must be deterministically verified by a human.

Author: aae_pl2 часа назад

Source: habr.com