Building an AI agent involves more than connecting an LLM to a prompt and providing tool access. An effective agent must interpret instructions, plan tasks, select tools, manage context, handle failures, and deliver verifiable results. This article presents an AI Agents engineering cheat sheet, outlining key concepts in architecture, Python implementation, frameworks, MCP, memory, RAG, evaluation, security, and deployment, all using free and open-source tools.
AI Agents Engineering Cheat Sheet
Whether you are building agents for professional projects, a portfolio, or a product, this guide provides a comprehensive overview I found valuable when starting out. It addresses all key components using only free tools, including Ollama for local models, LangGraph, CrewAI, Chroma, and open-source MCP.
Make sure to install:
ollama pull llama3.2
ollama pull nomic-embed-text
pip install ollama langgraph crewai chromadb "mcp[cli]"
Agent Architecture and the Reasoning Loop
An agent operates as an LLM running in a loop with four components: a model, tools, memory, and a stop condition. Most agents follow the ReAct loop: the model reasons, selects an action, observes the result, and repeats until it can respond. A common mistake is omitting a step limit, which led to issues in my own forty-call agent.
import ollama
def run_agent(task, tools, max_steps=6):
messages = [{"role": "user", "content": task}]
lookup = {t.__name__: t for t in tools}
for _ in range(max_steps):
reply = ollama.chat(model="llama3.2", messages=messages, tools=tools).message
messages.append(reply)
if not reply.tool_calls: # no action requested -> final answer
return reply.content
for call in reply.tool_calls:
result = lookup[call.function.name](**call.function.arguments)
messages.append({"role": "tool", "content": str(result),
"tool_name": call.function.name})
return "Stopped: step limit reached."Planning
For multi-step tasks, prompt the model to generate a brief plan before executing each step sequentially. Plan-and-execute is more cost-effective and predictable than pure ReAct, and allows for replanning if a step fails. Use this approach when the process is mostly defined. Use standard ReAct when subsequent actions depend on recent findings. A practical strategy is to assign simple steps to a smaller local model and reserve a larger model for planning.
Tool Calling
The model does not execute actions directly; it generates structured requests that your code processes. Tool quality directly impacts agent performance: use clear names, concise docstrings, and typed arguments. Limit the toolset to fewer than ten tools, and return brief, readable errors to enable recovery rather than repeated loops. Avoid returning entire database responses; provide only the information required for the next step.
def get_word_count(text: str) -> int:
"""Return the number of words in the given text."""
return len(text.split())
print(run_agent("How many words are in 'agents need stop conditions'?", [get_word_count]))Before You Go Deeper Into Agents
Building agents can help you stand out, but you must be prepared to explain your architectural decisions, failure handling strategies, and trade-offs in interviews. For practice, refer to my guide, Cracking your First AI/ML Interview.
MCP (Model Context Protocol)
MCP provides a standardized method for exposing tools, allowing any compatible client to access them. Develop a tool server once and reuse it across agents and frameworks, eliminating the need to rewrite integrations.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("notes")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
if __name__ == "__main__":
mcp.run() # stdio transportMemory
Short-term memory consists of message history, which should be trimmed or summarized before reaching the context window limit. Long-term memory involves storing information externally and retrieving it as needed.
import json, pathlib
STORE = pathlib.Path("memory.json")
def remember(key: str, value: str):
data = json.loads(STORE.read_text()) if STORE.exists() else {}
data[key] = value
STORE.write_text(json.dumps(data))
def recall() -> str:
return STORE.read_text() if STORE.exists() else "{}"RAG
Retrieval-Augmented Generation retrieves relevant text and incorporates it into the prompt. Divide documents into 200 to 500 token chunks with overlap, retrieve the most relevant sections, and instruct the model to respond with “I don’t know” if the context does not provide an answer. In agentic RAG, retrieval functions as a tool the agent uses only when necessary.
import chromadb, ollama
col = chromadb.Client().create_collection("docs")
docs = ["LangGraph models agents as graphs.", "CrewAI uses role-based agents."]
embed = lambda t: ollama.embed(model="nomic-embed-text", input=t).embeddings
col.add(ids=["1", "2"], documents=docs, embeddings=embed(docs))
def retrieve(query: str) -> str:
"""Search the knowledge base."""
hits = col.query(query_embeddings=embed([query]), n_results=1)
return hits["documents"][0][0]Multi-Agent Orchestration
Common patterns include supervisor-and-worker, sequential handoff, and parallel collaboration. It is best to begin with a single agent and divide responsibilities only when one prompt becomes overloaded with tools or tasks. LangGraph offers explicit control through state management and checkpoints, while CrewAI enables faster setup with role-based agents.
# LangGraph: researcher -> writer
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
import ollama
class State(TypedDict):
task: str
notes: str
answer: str
def ask(prompt):
return ollama.chat(model="llama3.2",
messages=[{"role": "user", "content": prompt}]).message.content
def researcher(s): return {"notes": ask(f"List key facts about: {s['task']}")}
def writer(s): return {"answer": ask(f"Write a short summary using: {s['notes']}")}
g = StateGraph(State)
g.add_node("researcher", researcher); g.add_node("writer", writer)
g.add_edge(START, "researcher"); g.add_edge("researcher", "writer"); g.add_edge("writer", END)
print(g.compile().invoke({"task": "AI agent memory"})["answer"])The Takeaway
Early in my experience building agents, I focused on frameworks. However, real progress came from understanding the underlying loop: reason, act, observe, and stop. While frameworks evolve frequently, this foundational process remains constant.
Start by building a small agent from scratch, adding one capability at a time and measuring each change. For those new to AI, this approach offers practical learning, as all concepts discussed can be implemented locally. Reliable agents result from consistent engineering practices, not from shortcuts.
Thank you for reading this article on the AI Agents engineering cheat sheet. For additional insights on AI and machine learning, please follow me on Instagram.





