LLM Agents
Planning and Reasoning in Agents â Thinking Before Acting
Effective agents do not just react â they plan. Planning enables agents to break down complex goals into manageable steps, anticipate obstacles, and adapt their strategy when things go wrong.
- Task Decomposition â Break complex goals into actionable steps
- ReAct â Interleave reasoning and action for grounded decisions
- Tree-of-Thought â Explore multiple reasoning paths in parallel
Failing to plan is planning to fail.
Planning and Reasoning in Agents
Planning is the ability to formulate a sequence of actions to achieve a goal. For LLM agents, planning involves decomposing tasks, reasoning about dependencies, and selecting the best action at each step.
Task Decomposition
Chain-of-Thought Planning
def chain_of_thought_plan(goal, llm, tools):
"""Generate a step-by-step plan using chain-of-thought reasoning."""
prompt = f"""Create a detailed plan to achieve this goal:
Goal: {goal}
Available tools: {', '.join(tools.keys())}
Think step by step:
1. What information do I need?
2. What actions should I take?
3. What is the order of operations?
Plan:"""
response = llm.generate(prompt)
steps = parse_plan(response)
return steps
Hierarchical Task Network
def htn_decompose(task, llm, max_depth=3):
"""Hierarchically decompose a task."""
if max_depth == 0 or is_primitive(task):
return [task]
prompt = f"""Break this task into subtasks:
Task: {task}
Subtasks (one per line):"""
response = llm.generate(prompt)
subtasks = [s.strip() for s in response.split("\n") if s.strip()]
# Recursively decompose each subtask
all_steps = []
for subtask in subtasks:
all_steps.extend(htn_decompose(subtask, llm, max_depth - 1))
return all_steps
ReAct: Reasoning + Acting
The ReAct Loop
Thought: I need to find the population of France.
Action: search("population of France 2024")
Observation: France has a population of approximately 68 million.
Thought: Now I need to find the GDP.
Action: search("GDP of France 2024")
Observation: France's GDP is approximately $3.0 trillion.
Thought: I now have both pieces of information.
Action: finish("France has 68 million people and $3.0T GDP.")
def react_agent(goal, llm, tools, max_steps=10):
"""ReAct agent that interleaves reasoning and action."""
steps = []
context = f"Goal: {goal}\n\n"
for i in range(max_steps):
# Generate thought and action
prompt = context + "Thought:"
thought = llm.generate(prompt)
prompt += f" {thought}\nAction:"
action = llm.generate(prompt)
# Execute action
if action.startswith("finish("):
answer = action[8:-2]
return answer, steps
# Parse and execute tool call
tool_name, tool_input = parse_action(action)
observation = tools[tool_name].execute(tool_input)
# Update context
context += f"Thought: {thought}\nAction: {action}\nObservation: {observation}\n"
steps.append({"thought": thought, "action": action, "observation": observation})
return "Max steps reached", steps
Tree-of-Thought
def tree_of_thought(problem, llm, num_branches=3, max_depth=4):
"""Explore multiple reasoning paths using tree search."""
def evaluate_path(path):
"""Evaluate how promising a reasoning path is."""
prompt = f"""Evaluate this reasoning path for solving: {problem}
Path: {' -> '.join(path)}
Score (0-1, where 1 is most promising):"""
score = float(llm.generate(prompt))
return score
def expand_path(path):
"""Generate next steps for a path."""
prompt = f"""Given this problem: {problem}
Current reasoning: {' -> '.join(path)}
Generate {num_branches} possible next steps:"""
response = llm.generate(prompt)
next_steps = parse_steps(response)
return [path + [step] for step in next_steps]
# BFS with pruning
current_paths = [[]]
for depth in range(max_depth):
all_candidates = []
for path in current_paths:
all_candidates.extend(expand_path(path))
# Evaluate and keep top paths
scored = [(path, evaluate_path(path)) for path in all_candidates]
scored.sort(key=lambda x: x[1], reverse=True)
current_paths = [path for path, score in scored[:num_branches]]
# Return best path
best_path = max(current_paths, key=evaluate_path)
return best_path
Goal-Conditioned Planning
GOFAI-Style Planning
class GoalConditionedPlanner:
def __init__(self, llm, tools, state_tracker):
self.llm = llm
self.tools = tools
self.state = state_tracker
def plan(self, goal, current_state):
"""Generate a plan to reach the goal from current state."""
prompt = f"""Current state: {current_state}
Goal: {goal}
Available actions: {list(self.tools.keys())}
Generate a plan (list of actions) to reach the goal:"""
response = self.llm.generate(prompt)
plan = parse_plan(response)
return plan
def replan(self, goal, current_state, executed_actions, failure_reason):
"""Replan after a failure."""
prompt = f"""Previous plan failed.
Current state: {current_state}
Goal: {goal}
Executed actions: {executed_actions}
Failure reason: {failure_reason}
Generate a new plan considering the failure:"""
response = self.llm.generate(prompt)
new_plan = parse_plan(response)
return new_plan
Adaptive Planning
Plan-Execute-Observe Loop
def plan_execute_observe(goal, llm, tools, max_iterations=5):
"""Main agent loop with planning, execution, and observation."""
plan = None
observations = []
for i in range(max_iterations):
# Plan
if plan is None or needs_replan(observations):
plan = generate_plan(goal, observations, llm, tools)
# Execute next step
current_step = plan["steps"][0]
try:
result = execute_step(current_step, tools)
observations.append({"step": current_step, "result": result, "success": True})
plan["steps"] = plan["steps"][1:]
# Check if goal achieved
if goal_achieved(goal, observations):
return synthesize_answer(goal, observations, llm)
except Exception as e:
observations.append({"step": current_step, "error": str(e), "success": False})
plan = None # Trigger replanning
return "Could not achieve goal within iteration limit"
Practice Exercises
-
Chain-of-Thought Planning: Implement a CoT planner for a research task. How does the plan quality compare to direct execution?
-
ReAct Implementation: Build a ReAct agent with web search and calculator tools. Test on questions requiring multiple reasoning steps.
-
Tree-of-Thought: Implement ToT for a puzzle-solving task (e.g., 24 game). How many branches and depth are needed for good performance?
-
Adaptive Planning: Test your planner on tasks where initial plans fail. How quickly does the agent recover with replanning?
Key Takeaways
What to Learn Next
-> Chain-of-Thought Reasoning Teaching models to reason step by step.
-> LLM Agent Frameworks Building autonomous agents with LLMs.
-> Tool Use and Function Calling Teaching LLMs to use external tools.
-> Memory Systems for Agents Long-term and short-term memory for agents.
-> Reinforcement Learning Training agents through reward signals.
-> Multi-Agent Systems Coordinating multiple agents.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions