0Pricing
AI Engineering Academy · 강의

사고 연쇄와 단계별 추론

사고 연쇄 프롬프트를 사용해 모델이 복잡한 문제를 단계별로 풀도록 안내하고, 수학, 논리, 다단계 추론 작업의 정확도를 크게 높입니다.

사고 연쇄와 단계별 추론은(는) CoddyKit의 무료 AI Engineering Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 AI Engineering Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. AI Engineering Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Is Chain-of-Thought Prompting?

Chain-of-Thought (CoT) prompting is a technique where you ask the model to show its reasoning step by step before giving a final answer. Instead of jumping straight to the conclusion, the model produces intermediate reasoning steps that lead to the answer. This simple change dramatically improves accuracy on complex tasks involving math, logic, and multi-step reasoning.

The technique was introduced in a 2022 Google Brain paper that showed CoT prompting could enable large language models to solve math word problems that they completely failed at with direct prompting. The key insight is that generating intermediate steps forces the model to allocate more computation to hard problems.

The Magic of 'Let's Think Step by Step'

The simplest form of chain-of-thought prompting is adding the phrase 'Let's think step by step' to your prompt. This zero-shot CoT trigger was discovered by researchers who found that this single phrase caused models to spontaneously produce reasoning chains even without few-shot examples.

import openai

client = openai.OpenAI()

# Without CoT - the model often gets multi-step math wrong
direct_prompt = 'A store has 48 apples. They sell 3/4 of them, then receive a new shipment of 20. How many apples do they have?'

# With zero-shot CoT
cot_prompt = direct_prompt + "\n\nLet's think step by step."

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': cot_prompt}]
)
print(response.choices[0].message.content)

Why CoT Works: Computation Allocation

LLMs generate text one token at a time, and each token is produced with a fixed amount of computation (one forward pass of the model). When you ask for a direct answer, the model must arrive at the correct answer in a single inference step, which is often too little computation for complex problems.

Chain-of-thought gives the model token budget to work with the problem. Each reasoning step is a token, and generating those tokens is equivalent to additional computation. This is why CoT works even on tasks where the model clearly knows all the individual facts — the bottleneck is not knowledge but the ability to combine facts over multiple reasoning steps in a single pass.

Few-Shot Chain-of-Thought

For maximum reliability, combine few-shot examples with chain-of-thought reasoning. Each example shows the full reasoning chain that leads to the correct answer. This teaches the model both the reasoning style you want and the correct format for the final answer.

import openai

client = openai.OpenAI()

prompt = '''Solve these math problems step by step.

Problem: A recipe needs 2.5 cups of flour for 12 cookies. How much flour for 30 cookies?
Reasoning: First, find flour per cookie: 2.5 / 12 = 0.208 cups each.
Then multiply by 30: 0.208 * 30 = 6.25 cups.
Answer: 6.25 cups

Problem: A train travels 180 km in 2.5 hours. At the same speed, how long to travel 270 km?
Reasoning: First, find speed: 180 / 2.5 = 72 km/h.
Time = distance / speed = 270 / 72 = 3.75 hours.
Answer: 3.75 hours

Problem: A shirt costs $45 after a 25% discount. What was the original price?
'''

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': prompt}]
)
print(response.choices[0].message.content)

CoT for Multi-Hop Reasoning

Chain-of-thought is not limited to math. It dramatically improves performance on multi-hop reasoning tasks where you must connect several facts to answer a question. For example, answering 'Who was the president of the US when the iPhone was invented?' requires knowing when the iPhone was invented, then looking up who was president at that time — two separate knowledge lookups chained together.

Without CoT, the model must short-circuit this chain in a single step and often produces the wrong answer. With CoT, it explicitly retrieves each fact in a reasoning step, dramatically improving accuracy on these tasks.

Structuring the Output with CoT

In production applications, you often want the reasoning to be separate from the final answer so you can parse the answer programmatically. A common pattern is to instruct the model to wrap reasoning in tags and the final answer in its own section, making it easy to extract the answer without parsing the full reasoning text.

import openai
import re

client = openai.OpenAI()

prompt = '''Analyze the following customer complaint and determine the priority level.
First think through the issue, then state your final classification.

Format your response exactly as:
<reasoning>
Your step-by-step analysis here.
</reasoning>
<priority>HIGH | MEDIUM | LOW</priority>

Complaint: "The checkout page crashes every time I try to buy something. I have tried three times and lost my cart each time."
'''

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': prompt}]
)
content = response.choices[0].message.content
priority = re.search(r'<priority>(.*?)</priority>', content).group(1)
print('Priority:', priority)

Self-Consistency: Vote Across Multiple Chains

Self-consistency is an extension of CoT that generates multiple independent reasoning chains for the same problem using a high temperature, then takes a majority vote over the final answers. The intuition is that correct reasoning paths tend to converge on the same answer, while incorrect paths tend to diverge.

This significantly improves accuracy on math and commonsense reasoning benchmarks at the cost of additional API calls. It is most useful when accuracy matters more than latency or cost, such as in scientific or financial applications where a wrong answer has serious consequences.

import openai
from collections import Counter

client = openai.OpenAI()

def self_consistent_answer(question, n=5):
    prompt = question + '\n\nLet\'s think step by step. State your final answer as: Answer: <value>'
    answers = []
    for _ in range(n):
        resp = client.chat.completions.create(
            model='gpt-4o-mini',
            messages=[{'role': 'user', 'content': prompt}],
            temperature=0.7
        )
        text = resp.choices[0].message.content
        # Extract final answer
        if 'Answer:' in text:
            ans = text.split('Answer:')[-1].strip().split('\n')[0]
            answers.append(ans)
    # Majority vote
    return Counter(answers).most_common(1)[0]

result, count = self_consistent_answer('What is 15% of 240?')
print(f'Answer: {result} (agreed by {count}/5 chains)')

Tree of Thought: Exploring Multiple Paths

Tree of Thought (ToT) extends chain-of-thought by exploring multiple reasoning branches simultaneously and evaluating which branch is most promising before continuing. Think of it like a search algorithm: rather than committing to one reasoning path, you generate several partial thoughts, score them, and expand the most promising ones.

This is particularly useful for planning and open-ended problems where the optimal reasoning path is not immediately clear. ToT requires significantly more API calls than basic CoT but can solve problems that linear CoT fails on, such as multi-step puzzles and creative problem-solving tasks.

When Not to Use Chain-of-Thought

CoT adds latency and token cost to every request. It is not always the right choice. Avoid CoT when:

  • The task is simple and the model reliably gets it right without reasoning (e.g., single-step classification)
  • You need very low latency and cannot afford extra tokens
  • The reasoning itself would confuse downstream parsing (though structured output tags solve this)
  • You are using a very small model that does not benefit from reasoning steps

CoT provides the most value on complex, multi-step tasks where the model demonstrably fails without it. Always measure before adding it to a production prompt.

CoT for Code and Debugging

Chain-of-thought is highly effective for code generation and debugging tasks. Asking the model to explain its plan before writing code — 'First explain your approach, then write the code' — produces more correct implementations because the planning step catches logical errors before they are encoded in the implementation.

For debugging, prompting the model to 'trace through the code step by step and identify where the output diverges from the expected value' is far more effective than just asking 'what is wrong with this code?' The step-by-step trace acts as a simulation of code execution within the model's reasoning.

Combining CoT with System Prompts

You can encode chain-of-thought instructions permanently in your system prompt so that every user message benefits from step-by-step reasoning without users needing to request it explicitly. This is the standard pattern for production applications where you want reliable CoT behavior across all queries.

import openai

client = openai.OpenAI()

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[
        {
            'role': 'system',
            'content': ('You are a precise analytical assistant. '
                       'For any question involving calculation, logic, or multi-step reasoning, '
                       'always think through the problem step by step before giving your answer. '
                       'Show your reasoning clearly before stating the final answer.')
        },
        {
            'role': 'user',
            'content': 'If I invest $5000 at 7% annual interest compounded annually, how much will I have after 10 years?'
        }
    ]
)
print(response.choices[0].message.content)

Quick Check

Test your understanding of AI Engineering concepts from this lesson.

Lesson Recap

In this lesson you learned: chain-of-thought prompting improves accuracy by making the model generate explicit reasoning steps before answering, self-consistency runs multiple chains and takes a majority vote for more reliable results, and CoT is most valuable for math, multi-hop reasoning, and debugging tasks, but adds latency and cost. Next up we explore how to write effective system prompts and define AI personas.

자주 묻는 질문

“사고 연쇄와 단계별 추론” 강의는 무료인가요?

네 — “사고 연쇄와 단계별 추론” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 AI Engineering Academy 강의 전체를 잠금 해제할 수 있습니다. AI Engineering Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“사고 연쇄와 단계별 추론”에서 뭘 배우나요?

사고 연쇄 프롬프트를 사용해 모델이 복잡한 문제를 단계별로 풀도록 안내하고, 수학, 논리, 다단계 추론 작업의 정확도를 크게 높입니다. 브라우저에서 직접 실행하는 실습 코드로 AI Engineering Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

AI Engineering Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 AI Engineering Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“사고 연쇄와 단계별 추론” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 AI Engineering Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 AI Engineering Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 제로샷 및 퓨샷 프롬프트 작성
  2. 사고 연쇄와 단계별 추론
  3. 시스템 프롬프트와 페르소나 정의
  4. 프롬프트 반복 개선과 디버깅
← AI Engineering Academy(으)로 돌아가기