Local AI Local AI Advanced

Build a Local Agent Workflow

Chain reasoning + tool calls into a repeatable local pipeline

54 of 66

The agent loop

A minimal agent repeats this cycle:

  1. Plan
  2. Call a tool
  3. Observe the result
  4. Answer or plan again

Build it in Python

import requests, json

def ask_llm(messages, tools=None):
    payload = {"model": "local-model", "messages": messages}
    if tools:
        payload["tools"] = tools
    r = requests.post("http://localhost:1234/v1/chat/completions", json=payload)
    return r.json()["choices"][0]["message"]

def read_file(path):
    try:
        with open(path) as f:
            return f.read()
    except FileNotFoundError:
        return "File not found"

tools = [{
    "type": "function",
    "function": {
        "name": "read_file",
        "parameters": {
            "type": "object",
            "properties": {"path": {"type": "string"}},
            "required": ["path"]
        }
    }
}]

messages = [{"role": "user", "content": "What does main.py do?"}]
msg = ask_llm(messages, tools)

if msg.get("tool_calls"):
    args = json.loads(msg["tool_calls"][0]["function"]["arguments"])
    result = read_file(args["path"])
    messages.append(msg)
    messages.append({"role": "tool", "content": result, "tool_call_id": msg["tool_calls"][0]["id"]})
    final = ask_llm(messages)
    print(final["content"])

When local agents win

  • Private or air-gapped environments
  • High-volume tasks where API costs add up
  • Tight feedback loops with local files

When cloud agents win

  • Complex multi-step reasoning
  • Reliable tool calling
  • Tasks requiring the strongest models

Working out which model to run this on? See The Codex. Packaging it as a reusable skill? See The Armory.