Key Techniques for Prompt Engineering with LLMs: From Chain-of-Thought to ReAct

Key techniques for prompt engineering with LLMs include Chain-of-Thought (CoT), Tree-of-Thought (ToT), ReAct, and Self-Consistency, organized into a five-layer architecture spanning from basic instruction design to advanced reasoning and safety mitigation.

The aishwaryanr/awesome-generative-ai-guide repository serves as a comprehensive knowledge hub for generative AI practitioners, featuring the Applied LLMs Mastery 2024 course. Within the course materials located at free_courses/Applied_LLMs_Mastery_2024/week2_prompting.md, you'll find a systematic breakdown of prompt engineering techniques that drive accurate, relevant, and safe outputs from large language models.

Foundational Components of LLM Prompts

Every effective prompt begins with four core elements identified in the repository's Prompt Basics section: instruction, context, input data, and output indicator. These building blocks form the Foundation Layer of the prompting architecture.

For example, a text-classification prompt combines these elements to clearly communicate the task to the model. Without this structural clarity, advanced reasoning techniques lose effectiveness.

Chain-of-Thought and Automated Reasoning

The repository structures advanced reasoning into distinct patterns that transform how LLMs process complex tasks.

Zero-Shot and Few-Shot Chain-of-Thought

Chain-of-Thought (CoT) prompting elicits step-by-step reasoning by appending the phrase "Let's think step by step" to queries. In zero-shot CoT, this simple addition triggers the model to decompose problems without prior examples. Few-shot CoT enhances this by prepending exemplars demonstrating the desired reasoning format.


# Zero-shot Chain-of-Thought prompt

prompt = """\
Q: Why does ice float on water?

Let's think step by step:
1. """  # The model will fill in the reasoning steps.

Automatic Chain-of-Thought (Auto-CoT)

Auto-CoT reduces manual effort by using an LLM to generate its own demonstration chains. This technique eliminates the need for hand-crafted examples while maintaining the benefits of few-shot reasoning.

Tree-of-Thought and Graph-of-Thought Techniques

Moving beyond linear reasoning chains, Tree-of-Thought (ToT) allows models to explore multiple reasoning branches, backtrack when encountering dead ends, and evaluate alternative paths. This architecture mimics human problem-solving by maintaining a search space of possible thought processes.

Graph-of-Thought (GoT) extends this concept further by representing reasoning as a directed acyclic graph. Unlike tree structures, GoT captures non-linear thought patterns where ideas can converge and diverge, enabling more complex inference for tasks requiring synthesis across multiple knowledge domains.

Verification and Action-Oriented Patterns

The Verification and Action Layers address robustness and real-world applicability.

Self-Consistency Sampling

Self-Consistency improves reliability by sampling multiple reasoning paths and selecting the most frequent answer. This technique specifically targets arithmetic and commonsense reasoning tasks where single-path generation might produce hallucinated or incorrect conclusions.

ReAct (Reason + Act)

The ReAct framework interleaves reasoning traces with external actions, such as web searches or API calls. By combining internal thought processes with tool use, ReAct overcomes knowledge gaps and reduces hallucinations in scenarios requiring up-to-date or proprietary information.


# ReAct pattern with reasoning and external tool call

react_prompt = """\
You are an AI assistant with access to a search tool.
Task: Find the current CEO of OpenAI.

Thought: I should look this up to ensure it is up‑to‑date.
Action: SEARCH[OpenAI CEO]
Observation: Sam Altman

Answer: The current CEO of OpenAI is Sam Altman.
"""

Safety and Risk Mitigation

The Safety Layer addresses prompt injection, leaking, jailbreaking, and bias. The repository emphasizes prompt sanitization and defensive prompting strategies to guard against adversarial inputs that could override system instructions or extract sensitive training data.

Essential Tools for Prompt Engineering

The week2_prompting.md file catalogs open-source utilities including PromptAppGPT, PromptBench, and Prompt Engine for building, testing, and evaluating prompts. These tools support the iterative development required when implementing the layered architecture described above.

Summary

  • Foundation Layer: Structure prompts with clear instruction, context, input data, and output indicators.
  • Reasoning Layer: Implement CoT, Auto-CoT, ToT, and GoT to enable multi-step and non-linear problem solving.
  • Verification Layer: Apply Self-Consistency sampling to improve answer reliability across multiple reasoning paths.
  • Action Layer: Use ReAct to combine reasoning with external tool calls for real-time knowledge retrieval.
  • Safety Layer: Sanitize inputs and implement defensive measures against prompt injection and jailbreaking attempts.

Frequently Asked Questions

What is the difference between zero-shot and few-shot Chain-of-Thought prompting?

Zero-shot Chain-of-Thought requires no examples, instead using trigger phrases like "Let's think step by step" to elicit reasoning. Few-shot Chain-of-Thought provides concrete exemplars demonstrating the desired reasoning format, which typically yields higher accuracy on complex tasks but requires manual curation or Auto-CoT generation.

How does Tree-of-Thought differ from Chain-of-Thought in LLM prompting?

Tree-of-Thought (ToT) extends Chain-of-Thought by allowing the model to explore multiple reasoning branches simultaneously and backtrack from incorrect paths, while CoT follows a single linear reasoning sequence. ToT essentially transforms the reasoning process into a search problem over a tree structure, improving performance on tasks requiring exploration of alternatives.

What is the ReAct framework and when should I use it?

ReAct combines reasoning traces with actionable steps, enabling LLMs to interact with external tools like search engines or APIs. Use ReAct when your application requires real-time information, proprietary database access, or verification against external knowledge sources that exceed the model's static training data.

How can I prevent prompt injection attacks when engineering prompts for LLMs?

Implement prompt sanitization by filtering user inputs for adversarial patterns, use delimiter-based separation between instructions and untrusted content, and apply defensive prompting techniques that explicitly instruct the model to ignore attempts to override system instructions. The repository's safety section in week2_prompting.md details specific mitigation strategies for injection, leaking, and jailbreaking attempts.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →