Agent Workflows with Ollama
What This Article Covers
- What AI agents are and how they work.
- How agents handle tools.
- Building multi-step workflows with Ollama.
- Memory, planning, and feedback loops.
- Practical Python example.
Introduction: Agent Workflows with Ollama
An AI agent is more than a simple language model. An agent pursues a goal, decides which tools to use, evaluates results, and adapts its approach. Ollama provides a solid foundation for such agents because models run locally and tools can be accessed easily through prompts. When you build multi-step workflows, you can automate complex tasks like research, data processing, or code generation.
This article shows how to implement a simple yet functional agent workflow with Ollama.
Key Concepts
- Agent: A system that pursues goals and plans actions.
- Tool: An external function the agent calls.
- ReAct: Reasoning + Acting, alternating between thought and action.
- Memory: Storage of past actions and results.
- Planning: Step-by-step goal achievement.
- Feedback Loop: Checking results and adjusting approach.
- Observation: What the agent observes after an action.
- Thought: A reasoning step by the agent.
Agent Workflow at a Glance
A typical workflow looks like this:
- Define the goal.
- Create a plan.
- Execute the first step or call a tool.
- Observe the result.
- Plan the next step.
- Repeat until the goal is reached or max steps are hit.
- Return the final answer.
Simple ReAct Workflow
import requests
import json
def ollama_chat(prompt, model="llama3.1"):
url = "http://localhost:11434/api/generate"
payload = {
"model": model,
"prompt": prompt,
"stream": False
}
response = requests.post(url, json=payload)
return response.json()["response"].strip()
def tool_recherche(query):
return f"Ergebnisse zu {query}: Seite 1, Seite 2, Seite 3."
def tool_rechner(ausdruck):
try:
return str(eval(ausdruck))
except:
return "Fehler"
prompt = """Der Assistent ist ein Agent. Erstelle die nächste Aktion im Format:
Aktion: [SUCHE] Thema
oder
Aktion: [RECHNE] 2+2
Ziel: Wie viele Seiten hat der BotServ-Artikel "Ollama Befehle"?"""
antwort = ollama_chat(prompt)
print(antwort)
Registering Tools
tools = {
"SUCHE": tool_recherche,
"RECHNE": tool_rechner,
}
Multi-Step Workflow
def run_agent(ziel, max_steps=10):
gedaechtnis = []
for step in range(max_steps):
kontext = "\n".join(gedaechtnis)
prompt = f"""Ziel: {ziel}\nBisherige Schritte:\n{kontext}\n\nEntscheide die nächste Aktion. Format: Aktion: [NAME] Parameter"""
antwort = ollama_chat(prompt)
if "ENDE" in antwort:
return antwort.replace("Aktion: ENDE", "")
if antwort.startswith("Aktion:"):
_, rest = antwort.split(":", 1)
name, param = rest.strip().split(" ", 1)
name = name.replace("[", "").replace("]", "")
ergebnis = tools.get(name, lambda x: f"Unbekanntes Tool {name}")(param)
gedaechtnis.append(f"Schritt {step + 1}: {antwort}\nErgebnis: {ergebnis}")
else:
gedaechtnis.append(f"Schritt {step + 1}: {antwort}")
return "Maximale Schrittzahl erreicht."
print(run_agent("Suche nach Ollama Befehle und addiere die Anzahl der gelisteten Seiten"))
Memory
An agent needs memory to work coherently. Simple approaches include:
- A string containing previous steps in the prompt.
- A list that grows step by step.
- A vector database for longer conversations.
- A file or database for persistent storage.
Planning
Before execution, the model can create a plan:
plan_prompt = f"""Erstelle einen kurzen Plan aus max. 5 Schritten für folgendes Ziel:
Ziel: {ziel}
Format:
1. Schritt
2. Schritt
"""
plan = ollama_chat(plan_prompt)
print(plan)
Feedback Loops
After each step, the agent checks whether the result helps:
eval_prompt = f"""Bewerte kurz, ob dieses Ergebnis dem Ziel dient:
Ziel: {ziel}
Ergebnis: {ergebnis}
Antworte mit FORTFAHREN oder ZURUECKSETZEN."""
Designing Tools Well
- Clear names.
- Short descriptions.
- Strict parameter formats.
- Error handling during execution.
- Returns in a defined format.
Security
- Validate inputs before tool execution.
- Never use
evalwithout restrictions. - Run code tools in isolation.
- Don’t allow infinite loops.
- Keep logs.
- Don’t pass sensitive data to tools without protection.
Further Reading and Resources
- BotServ.de Ollama MCP
- BotServ.de Ollama Automation
- BotServ.de Ollama Prompt Engineering
- BotServ.de Agent Memory
FAQ: Agent Workflows
Do you need LangChain for agents? No, simple workflows work fine with Python and Ollama alone.
Can Ollama run complex agents? Yes, as long as the logic is clearly structured.
How many tools can be available at once? Multiple, but too many will overwhelm smaller models.
What’s the difference between RAG and agents? RAG retrieves knowledge, agents plan and execute actions.
Are agents slower? Yes, because multiple API calls are needed.
Sources and Further Reading
- ReAct Paper: https://arxiv.org/abs/2210.03629
- LangChain Agents: https://python.langchain.com/docs/modules/agents/
- Ollama API: https://github.com/ollama/ollama/blob/main/docs/api.md
Summary: Agent Workflows with Ollama
Agent workflows extend Ollama with planning, tool use, and feedback loops. With a well-structured prompt, memory, and a few simple tools, you can build a functional agent. Key to success are strict action formats, input validation, and a step limit to prevent infinite loops. For complex applications, frameworks like LangChain help, but for many use cases a lightweight, custom implementation works just fine.


