RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Custom Agent Frameworks
  6. /Ch. 2
Custom Agent Frameworks

02. Agent Runtime Design

Chapter 2 of 24 · 15 min
KEY INSIGHT

The runtime loop is a controlled iteration pattern. Every component has a single responsibility, and the orchestrator coordinates their interaction through a defined message protocol.

An agent runtime has four core components that interact in a defined order. Understanding this topology is essential before writing any code.

Components:

  1. Orchestrator — Controls flow. Decides what happens next, when to loop, when to stop.
  2. LLM Interface — Wraps the model API. Handles prompt construction, response parsing, token counting.
  3. Tool Registry — Maps function names to callable implementations with schema definitions.
  4. Memory System — Manages state across the agent's lifetime (detailed in Chapters 6-9).

The orchestrator holds references to the others. It queries the LLM, parses tool calls, dispatches through the registry, updates memory, and repeats until completion.

class AgentRuntime:
    def __init__(
        self,
        llm: LLMInterface,
        tools: ToolRegistry,
        memory: MemorySystem,
        max_iterations: int = 20
    ):
        self.llm = llm
        self.tools = tools
        self.memory = memory
        self.max_iterations = max_iterations

    async def run(self, prompt: str) -> AgentResponse:
        self.memory.add_message(role="user", content=prompt)
        
        for iteration in range(self.max_iterations):
            response = await self.llm.chat(
                messages=self.memory.get_context(),
                tools=self.tools.schemas()
            )
            
            if response.finish_reason == "stop":
                return AgentResponse(finish_reason="stop", content=response.content)
            
            if response.finish_reason == "tool_use":
                results = await self.tools.execute(response.tool_calls)
                self.memory.add_tool_results(response.tool_calls, results)
                continue
            
            # Handle unexpected finish reasons
            return AgentResponse(
                finish_reason="max_iterations" if iteration == self.max_iterations - 1 
                else response.finish_reason,
                content=response.content
            )

Failure mode: unbounded iteration. Without max_iterations, a buggy tool that always calls itself creates an infinite loop. Production systems must cap iterations and emit alerts when the cap is hit.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Draw a diagram of your target agent's runtime topology. Label which components you need to build versus reuse. Note any integration points that could become bottlenecks.

← Chapter 1
Why Custom Frameworks?
Chapter 3 →
Agent Loop