في هذه الصفحة
الدّرس 4 من 6
سلسلة التّفكير
ما ستتعلّمه
- إثارة سلسلة التّفكير بنظافة (لا بترجّي النّموذج أن يفكّر)
- تركيب سلسلة التّفكير مع المخرج المنظّم
- تفادي الحالات التي تضرّ فيها
Chain-of-thought (CoT) is a prompting technique that makes the model reason step by step before producing a final answer. It is one of the most well-studied techniques in prompt engineering, and for good reason: on tasks that involve logic, math, or multi-step reasoning, CoT can dramatically improve accuracy.
What CoT actually does
When you ask a model a complex question directly, it generates the answer token by token, committing to each token as it goes. If the first token of the answer points in the wrong direction, the rest follows. CoT changes this by giving the model space to work through the problem before committing to an answer.
Consider this without CoT:
Q: A store sells apples for $2 each. If you buy 5 or more,
you get 20% off. How much do 7 apples cost?
A: $14
Wrong. The model jumped straight to 7 x $2 without applying the discount. Now with CoT:
Q: A store sells apples for $2 each. If you buy 5 or more,
you get 20% off. How much do 7 apples cost?
Think step by step before answering.
A: Let me work through this:
1. Base price: 7 × $2 = $14
2. Since 7 ≥ 5, the 20% discount applies
3. Discount: $14 × 0.20 = $2.80
4. Final price: $14 - $2.80 = $11.20
The answer is $11.20.
The reasoning trace forces the model to surface the discount condition before producing the final number.
How to trigger CoT
There are several ways to trigger chain-of-thought reasoning, ranging from simple to structured.
Simple trigger phrases:
- "Think step by step."
- "Before answering, reason through the problem."
- "Show your work."
These work but are imprecise. The model decides how much reasoning to show.
Structured CoT with XML tags:
For production use, structure the reasoning explicitly:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-0",
max_tokens=1024,
system="""You are an underwriting assistant. For each application,
analyze risk factors step by step, then produce a decision.""",
messages=[
{
"role": "user",
"content": """Evaluate this insurance application:
- Age: 34
- Smoker: No
- BMI: 27
- Family history: Father had heart disease at 62
- Occupation: Software engineer (sedentary)
Structure your response as:
<reasoning>
[Your step-by-step analysis here]
</reasoning>
<decision>
{"risk_level": "low|medium|high", "approved": true|false, "notes": "string"}
</decision>"""
}
]
)
This approach is powerful because you get the reasoning (useful for audits and debugging) separated from the structured output (useful for your code).
When CoT helps
CoT is most effective for:
- Math and arithmetic — any time the model needs to compute intermediate values.
- Multi-step logic — "If A and B, then C, therefore D" reasoning chains.
- Code analysis — tracing execution flow or debugging logic errors.
- Ambiguous classification — when the answer depends on weighing multiple factors.
- Planning — breaking a complex task into ordered subtasks.
When CoT hurts
CoT is not free. It adds tokens (cost) and latency (time). There are cases where it actively makes things worse:
- Simple factual lookups — "What is the capital of France?" Adding CoT here wastes tokens and can even introduce doubt where none existed.
- Latency-sensitive paths — If you need a response in under 200ms for a user-facing feature, CoT reasoning can double or triple response time.
- Simple classification with clear examples — If you have good few-shot examples for a straightforward task, CoT adds overhead without improving accuracy.
- Creative writing — Asking a model to "think step by step" before writing a poem usually produces worse creative output than letting it write directly.
The rule of thumb: if a smart human would need a scratchpad to solve the problem, use CoT. If a smart human would answer instantly, skip it.
Combining CoT with structured output
The most production-ready pattern is: think first, then answer in a structured format. The XML tag approach shown above is Claude's strength. Here is the general pattern:
<thinking>
[Model reasons here — you can parse this for logging but don't use it as the answer]
</thinking>
<answer>
[Structured output here — this is what your code parses]
</answer>
In your application code, you extract only the content inside <answer> tags for downstream processing, while optionally logging the <thinking> block for debugging and quality monitoring.
Self-consistency: CoT at scale
For high-stakes decisions, you can run the same CoT prompt multiple times (with temperature > 0) and take the majority answer. This technique, called self-consistency, reduces the chance of a single bad reasoning path leading to a wrong answer. It costs N times as many tokens, so reserve it for cases where accuracy matters more than cost.
What's next
CoT helps the model reason. But reasoning in prose is not enough for production — your code needs to parse the output reliably. The next lesson, Structured output, teaches you how to get JSON, XML, and other machine-readable formats out of the model consistently.
سلسلة التّفكير (CoT) تقنيّة مطالبة تجعل النّموذج يستدلّ خطوة بخطوة قبل إنتاج إجابة نهائيّة. إنّها من أكثر التّقنيّات دراسةً في هندسة المطالبات: في المهامّ التي تتضمّن منطقًا أو رياضيّات أو استدلالًا متعدّد الخطوات، تحسّن سلسلة التّفكير الدّقّة بشكل ملحوظ.
ماذا تفعل سلسلة التّفكير فعلًا
حين تسأل النّموذج سؤالًا معقّدًا مباشرةً، يولّد الإجابة رمزًا برمز ملتزمًا بكلّ رمز فور إنتاجه. إن أشار الرّمز الأوّل في الاتّجاه الخاطئ، يتبعه الباقي. سلسلة التّفكير تغيّر هذا بمنح النّموذج مساحة لمعالجة المشكلة قبل الالتزام بالإجابة.
كيف تثير سلسلة التّفكير
عبارات بسيطة: "فكّر خطوة بخطوة"، "قبل الإجابة، استدلّ عبر المشكلة."
سلسلة تفكير منظّمة بوسوم XML: للاستعمال الإنتاجي، هيكل الاستدلال صراحةً باستعمال وسوم <reasoning> و<decision>. هذا يفصل الاستدلال (مفيد للتّدقيق) عن المخرج المنظّم (مفيد لكودك).
متى تساعد سلسلة التّفكير
- الرّياضيّات والحساب — كلّما احتاج النّموذج لحساب قيم وسيطة.
- المنطق المتعدّد الخطوات — سلاسل "إذا أ و ب، فـ ج، إذن د".
- تحليل الكود — تتبّع مسار التّنفيذ أو تنقيح أخطاء المنطق.
- التّصنيف الغامض — حين تعتمد الإجابة على موازنة عوامل متعدّدة.
متى تضرّ سلسلة التّفكير
- البحث عن حقائق بسيطة — إضافة سلسلة تفكير هنا تهدر الرّموز.
- المسارات الحسّاسة للتّأخير — سلسلة التّفكير قد تضاعف وقت الاستجابة.
- التّصنيف البسيط بأمثلة واضحة — الأمثلة الجيّدة تكفي بدون استدلال إضافي.
- الكتابة الإبداعيّة — طلب "فكّر خطوة بخطوة" قبل كتابة قصيدة يُنتج عادةً مخرجًا أسوأ.
القاعدة العامّة: إن كان إنسان ذكيّ يحتاج ورقة مسوّدة لحلّ المشكلة، استعمل سلسلة التّفكير. إن كان سيجيب فورًا، تخطّها.
دمج سلسلة التّفكير مع المخرج المنظّم
النّمط الأكثر جاهزيّة للإنتاج: فكّر أوّلًا، ثمّ أجب بشكل منظّم. في كود تطبيقك، تستخرج فقط المحتوى داخل وسوم الإجابة للمعالجة، مع تسجيل كتلة التّفكير اختياريًّا للتّنقيح ومراقبة الجودة.
ما التّالي
سلسلة التّفكير تساعد النّموذج على الاستدلال. لكنّ الاستدلال بالنّثر لا يكفي للإنتاج — كودك يحتاج لتحليل المخرج بشكل موثوق. الدّرس القادم، المخرَج المنظّم، يعلّمك كيف تحصل على JSON وXML وأشكال أخرى قابلة للقراءة الآليّة من النّموذج بانتظام.
جرّب بنفسك
خذ مشكلة متعدّدة الخطوات. شغّلها بسلسلة تفكير وبدونها. قس الزّمن. لاحظ الجودة مقابل التّأخير.
تأمّل
في حالات استعمالك المعتادة، هل تكلفة تأخير سلسلة التّفكير تستحقّ مكسب الدّقّة؟ أين الخطّ الفاصل؟