Module 7: Agents & Agentic Systems
Master autonomous AI Agents, function calling, tool use, memory systems, LangChain/CrewAI frameworks, and multi-agent coordination.
7.1 LLM Agents Foundations
An **LLM Agent** is an autonomous system driven by a core LLM. Instead of executing static prompts, an agent operates in a continuous loop:
- Perception: Receives input requests from the environment or user.
- Brain (LLM Planning): Analyzes the request, splits complex goals into smaller tasks, and decides which action to take.
- Tools (Actions): Executes tools (like searching the web, running code, querying database APIs).
- Observation: Evaluates tool outputs and repeats the loop until the goal is completed.
7.2 Memory Systems
Agents require memory configurations to handle multi-turn operations:
- Short-term Memory: The context window of the LLM containing active conversation histories and planning steps.
- Long-term Memory: Storing histories or episodic logs in external vector databases. The agent retrieves relevant memories from previous days or sessions dynamically.
- Episodic Memory: Specific records of past agent executions (e.g. "I ran this SQL script yesterday and got error X").
- Semantic Memory: General facts and world profiles learned during SFT or retrieved via RAG.
7.3 Tool Use & Function Calling
**Function Calling** is a protocol that allows the LLM to output a structured JSON arguments query rather than plain text, specifying which function to run and with what inputs.
Workflow:
- The developer provides the LLM with a list of available functions documented in a JSON Schema structure.
- The LLM decides a function needs to be called, parses input parameters, and outputs a formatted JSON string.
- The application code receives this JSON, runs the local function (e.g., executing a database query), and feeds the return value back to the LLM.
- The LLM summarizes the returned values into a natural language response.
import json
# 1. Define local tools
def get_stock_price(ticker):
stocks = {"GOOG": 175.50, "AAPL": 180.20}
return f"The price of {ticker} is ${stocks.get(ticker, 'unknown')}"
# 2. Simulated LLM Output requesting tool use
llm_json_call = '{"tool": "get_stock_price", "args": {"ticker": "GOOG"}}'
# 3. Application router executes function
call_info = json.loads(llm_json_call)
func_name = call_info["tool"]
arguments = call_info["args"]
if func_name == "get_stock_price":
result = get_stock_price(arguments["ticker"])
print("Executed Tool Output:", result)
7.4 Agent Frameworks
Developers use frameworks to build complex agent applications:
- LangChain: Provides modular abstractions for chains, prompt templates, tool connections, and simple agents.
- LlamaIndex: Excellent for data-centric agents, specializing in RAG retrievals and document integrations.
- CrewAI: Simplifies task assignments, role personas, and worker collaborations.
7.5 Multi-Agent Systems
Complex jobs are best solved by a team of specialized agents (multi-agent systems) collaborating with each other.
- Orchestrator-Worker Pattern: A central orchestrator agent decomposes a task, delegates sub-tasks to worker agents (e.g. Researcher, Coder, Writer), collects their inputs, and compiles the final result.
- Agentic Debate: Multiple agents generate draft solutions, critique each other's outputs, and iterate recursively to reach a consensus, reducing errors.
- Model Context Protocol (MCP): An open standard developed by Anthropic that allows agents to safely connect to files, database engines, and dev environments using a unified client-server architecture.
7.6 Planning & Reasoning
Autonomous agents plan execution paths dynamically:
- Task Decomposition: Splitting a large goal ("Write a full game in Python") into linear steps (Design, Code, Test, Debug).
- Self-Correction & Reflection: When a tool fails (e.g. a Python execution returns a SyntaxError), the agent reads the error traceback, reflects on the bug, edits its script, and re-executes, correcting its own errors without human intervention.
Next Steps
Proceed to Module 8: Diffusion Models & Image Generation to learn about generative computer vision.
