VOOZH about

URL: https://thenewstack.io/how-to-add-reasoning-to-ai-agents-via-prompt-engineering/

⇱ How To Add Reasoning to AI Agents via Prompt Engineering - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-11-20 07:08:02
How To Add Reasoning to AI Agents via Prompt Engineering
tutorial,
AI / AI Engineering / Large Language Models

How To Add Reasoning to AI Agents via Prompt Engineering

Prompting strategies enhance an agent's reasoning capabilities, helping problem-solve for AI apps. We show you how to implement.
Nov 20th, 2024 7:08am by Janakiram MSV
👁 Featued image for: How To Add Reasoning to AI Agents via Prompt Engineering
Image via Unsplash+. 
More from this series on AI agent development (download all the code from GitHub):
– Overview: AI Agents: A Comprehensive Introduction for Developers
– Step 1: How To Define an AI Agent Persona by Tweaking LLM Prompts
– Step 2: Enhancing AI Agents: Adding Instructions, Tasks and Memory
– Step 3: Enhancing AI Agents: Implementing Reasoning Through Prompt Engineering
– Step 4: How To Add Persistence and Long-Term Memory to AI Agents
– Step 5: How To Add RAG to AI Agents for Contextual Understanding
– Step 6: How To Add Tool Support to AI Agents for Performing Actions

In our previous exploration of AI agent architecture, we discussed the core components of persona, instructions and memory. Now, we’ll delve into how different prompting strategies enhance an agent’s reasoning capabilities, making them more methodical and transparent in their problem-solving approach.

Effective prompt engineering techniques have proven crucial in helping Large Language Models (LLMs) produce more reliable, structured, and well-reasoned responses. These techniques leverage several key principles:

  • Step-by-Step Decomposition: Breaking down complex tasks into smaller, manageable steps helps LLMs process information more systematically, reducing errors and improving logical consistency.
  • Explicit Format Instructions: Providing clear output structures guides the model to organize its thoughts and present information in a more digestible format.
  • Self-Reflection Prompts: Encouraging the model to review its own reasoning process helps catch potential errors and consider alternative perspectives.
  • Contextual Frameworks: Offering specific frameworks (like “analyze pros and cons” or “consider multiple scenarios”) helps the model approach problems from different angles.

These techniques form the foundation for our implemented reasoning strategies, each designed to capitalize on different aspects of LLM capabilities while maintaining consistency and reliability in responses.

Understanding Strategy-Based Reasoning

While basic agents can process tasks directly, advanced reasoning requires structured approaches to problem-solving. The implementation uses a strategy pattern that defines different reasoning frameworks. Let’s look at how these strategies are defined in our enhanced agent architecture:

class ExecutionStrategy(ABC):
 @abstractmethod
 def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
 """Build the prompt according to the strategy."""
 pass

 @abstractmethod
 def process_response(self, response: str) -> str:
 """Process the LLM response according to the strategy."""
 pass

This abstract base class provides the foundation for implementing various reasoning strategies. Each strategy offers a unique approach to:

  • Structuring the problem-solving process;
  • Breaking down complex tasks;
  • Organizing the agent’s thought process; and
  • Ensuring thorough consideration of the problem.

Let’s take a closer look at three different techniques: ReAct, Chain of Thought, and Reflection. The framework makes it easy to add other techniques, too.

ReAct: Reasoning and Acting

The ReAct strategy (Reasoning and Action) implements a cycle of thought, action, and observation, making the agent’s decision-making process explicit and traceable. Here’s how it’s implemented:

class ReactStrategy(ExecutionStrategy):
 def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
 base_prompt = """Approach this task using the following steps:
1) Thought: Analyze what needs to be done
2) Action: Decide on the next action
3) Observation: Observe the result
4) Repeat until task is complete

Follow this format for your response:
Thought: [Your reasoning about the current situation]
Action: [The action you decide to take]
Observation: [What you observe after the action]
... (continue steps as needed)
Final Answer: [Your final response to the task]

Task: {task}"""

This strategy ensures that:

  • Explicit Reasoning: Each step of the thought process is clearly articulated.
  • Action-Based Approach: Decisions are tied to concrete actions.
  • Iterative Refinement: Solutions evolve through multiple cycles of observation and adjustment.

Chain of Thought: Step-by-Step Problem Solving

The Chain of Thought strategy breaks down complex problems into manageable steps, making the reasoning process more transparent and verifiable. Here’s what it looks like:

class ChainOfThoughtStrategy(ExecutionStrategy):
 def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
 base_prompt = """Let's solve this step by step:

Task: {task}

Please break down your thinking into clear steps:
1) First, ...
2) Then, ...
(continue with your step-by-step reasoning)

Final Answer: [Your conclusion based on the above reasoning]"""

This approach provides:

  • Linear progression through complex problems;
  • Clear connection between steps and conclusions;
  • Easier verification of the reasoning process; and
  • Better understanding of how conclusions are reached.

Reflection: Deep Analysis and Self-Review

The Reflection strategy adds a meta-cognitive layer, encouraging the agent to examine its own assumptions and consider alternative approaches. In code:

class ReflectionStrategy(ExecutionStrategy):
 def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
 base_prompt = """Complete this task using reflection:

Task: {task}

1) Initial Approach:
 - What is your first impression of how to solve this?
 - What assumptions are you making?

2) Analysis:
 - What could go wrong with your initial approach?
 - What alternative approaches could you consider?

3) Refined Solution:
 - Based on your reflection, what is the best approach?
 - Why is this approach better than the alternatives?"""

Integration With Agent Architecture

These strategies are seamlessly integrated into the agent architecture through a factory pattern and strategy setter:

class Agent:
 @property
 def strategy(self) -> Optional[ExecutionStrategy]:
 return self._strategy

 @strategy.setter
 def strategy(self, strategy_name: str):
 """Set the execution strategy by name."""
 self._strategy = StrategyFactory.create_strategy(strategy_name)

The execution flow incorporates the selected strategy:

 def execute(self, task: Optional[str] = None) -> str:
 if task is not None:
 self._task = task
 
 messages = self._build_messages()
 
 try:
 response = client.chat.completions.create(
 model=self._model,
 messages=messages
 )
 
 response_content = response.choices[0].message.content
 
 # Process response through strategy if set
 if self._strategy:
 response_content = self._strategy.process_response(response_content)

Practical Implementation

Here’s how these strategies are used in practice:

from agent import Agent

def main():
 # Initialize the agent
 agent = Agent("Problem Solver")
 
 # Configure the agent
 agent.persona = """You are an analytical problem-solving assistant.
You excel at breaking down complex problems and explaining your thought process.
You are thorough, logical, and clear in your explanations."""

 agent.instruction = "Ensure your responses are clear, detailed, and well-structured."

 # Define the park planning task
 park_planning_task = """
 A city is planning to build a new park. They have the following constraints:
 - Budget: $2 million
 - Space: 5 acres
 - Must include: playground, walking trails, and parking
 - Environmental concerns: preserve existing trees
 - Community request: include area for community events
 How should they approach this project?"""

 # Display available reasoning strategies
 print("Available reasoning strategies:", agent.available_strategies())
 print("\n" + "="*50)

 # Test ReAct strategy
 print("\n=== Using ReAct Strategy ===")
 agent.strategy = "ReactStrategy"
 agent.task = park_planning_task
 response = agent.execute()
 print(f"\nTask: {park_planning_task}")
 print("\nResponse:")
 print(response)
 print("\n" + "="*50)

 # Test Chain of Thought strategy
 print("\n=== Using Chain of Thought Strategy ===")
 agent.clear_history() # Clear previous interaction history
 agent.strategy = "ChainOfThoughtStrategy"
 agent.task = park_planning_task
 response = agent.execute()
 print(f"\nTask: {park_planning_task}")
 print("\nResponse:")
 print(response)
 print("\n" + "="*50)

 # Test Reflection strategy
 print("\n=== Using Reflection Strategy ===")
 agent.clear_history() # Clear previous interaction history
 agent.strategy = "ReflectionStrategy"
 agent.task = park_planning_task
 response = agent.execute()
 print(f"\nTask: {park_planning_task}")
 print("\nResponse:")
 print(response)
 print("\n" + "="*50)

if __name__ == "__main__":
 main()

This implementation allows for:

  • Flexible Strategy Selection: Different reasoning approaches for different types of tasks.
  • Consistent Format: Structured output regardless of the chosen strategy.
  • Clear Reasoning Trail: Transparent documentation of the problem-solving process.
  • Strategy Comparison: Easy evaluation of different approaches to the same problem.

Benefits of Strategic Reasoning

The implementation of these reasoning strategies brings several key advantages:

  • Enhanced Problem-Solving: Multiple approaches to tackle complex tasks.
  • Improved Transparency: Clear visibility into the agent’s reasoning process.
  • Better Verification: Easier validation of the agent’s conclusions.
  • Flexible Architecture: Easy addition of new reasoning strategies.

The entire source code for the framework is available in a GitHub repository.

Looking Ahead

While these reasoning strategies significantly enhance the agent’s capabilities, there are several areas for future improvement:

  • Dynamic strategy selection based on task type;
  • Hybrid approaches combining multiple strategies;
  • Enhanced error handling within each strategy; and
  • Metric-based evaluation of strategy effectiveness.

The combination of structured reasoning strategies with the agent’s existing capabilities creates a more powerful and versatile system capable of handling complex problems while maintaining transparency and reliability in its decision-making process.

In the next part of this series, we will add long-term memory to agents that enable them to pause and resume tasks. Stay tuned.

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.