Core Concepts
LLM as a Reasoning Engine
At the heart of every AI agent is a Large Language Model (LLM) acting as the "brain." Instead of generating text responses, the LLM is used to:
- Understand the user's goal
- Plan what steps are needed
- Decide which tools to use
- Evaluate results and adjust strategy
# Conceptual flow of an AI agent
def agent_loop(goal):
plan = llm.plan(goal) # Break goal into steps
for step in plan:
tool = llm.select_tool(step) # Decide which tool to use
result = tool.execute(step) # Execute the action
if llm.evaluate(result): # Check if step succeeded
continue
else:
plan = llm.replan(goal, result) # Adapt strategy
return final_result
The ReAct Pattern (Reasoning + Acting)
The most fundamental agentic pattern. The LLM alternates between thinking (reasoning) and doing (acting):
Thought: I need to find the current stock price of AAPL
Action: search_stock_price(ticker="AAPL")
Observation: AAPL is currently trading at $195.23
Thought: Now I need to compare it with last month's price
Action: get_historical_price(ticker="AAPL", days_ago=30)
Observation: 30 days ago AAPL was $187.45
Thought: I can now calculate the change and provide the answer
Final Answer: AAPL has risen $7.78 (4.15%) over the past 30 days.
Tool Use
Agents become powerful when they can interact with the outside world through tools:
from langchain.tools import tool
@tool
def search_web(query: str) -> str:
"""Search the web for current information."""
results = google_search_api(query)
return results
@tool
def execute_python(code: str) -> str:
"""Execute Python code and return the output."""
return exec_sandbox(code)
@tool
def query_database(sql: str) -> str:
"""Run a SQL query against the company database."""
return db.execute(sql)
@tool
def send_email(to: str, subject: str, body: str) -> str:
"""Send an email to the specified recipient."""
return email_client.send(to, subject, body)
Memory Systems
Agents need memory to maintain context across interactions:
Short-Term Memory (Conversation Buffer)
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
# Stores recent conversation turns
# Limited by context window size
Long-Term Memory (Vector Store)
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
# Store knowledge that persists across sessions
vectorstore = Chroma(embedding_function=OpenAIEmbeddings())
vectorstore.add_documents(company_docs)
# Agent can retrieve relevant info anytime
relevant_docs = vectorstore.similarity_search("Q4 revenue")
Episodic Memory (Past Experiences)
# Store successful action sequences for future reference
memory_store = {
"deploy_app": ["run tests", "build docker", "push to ECR", "update ECS"],
"debug_error": ["check logs", "identify root cause", "apply fix", "verify"]
}
Planning Strategies
Sequential Planning — Steps executed one after another:
Goal: "Write a blog post about AI trends"
Plan:
1. Research current AI trends (search web)
2. Outline key points (reasoning)
3. Write draft (text generation)
4. Review and edit (self-critique)
5. Format in markdown (tool use)
Hierarchical Planning — Break into sub-goals, each with their own plan:
Goal: "Launch new product feature"
├── Sub-goal 1: Design
│ ├── Research competitor features
│ ├── Create user stories
│ └── Design mockups
├── Sub-goal 2: Implement
│ ├── Write backend API
│ ├── Build frontend UI
│ └── Write tests
└── Sub-goal 3: Deploy
├── Set up CI/CD
├── Deploy to staging
└── Monitor and release