File size: 11,522 Bytes
e706de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Concept: Reasoning & Problem-Solving Agents

## Overview

This example demonstrates how to configure an LLM as a **reasoning agent** capable of analytical thinking and quantitative problem-solving. It shows the bridge between simple text generation and complex cognitive tasks.

## What is a Reasoning Agent?

A **reasoning agent** is an LLM configured to perform logical analysis, mathematical computation, and multi-step problem-solving through careful system prompt design.

### Human Analogy

```

Regular Chat                    Reasoning Agent

─────────────                  ──────────────────

"Can you help me?"            "I am a mathematician.

"Sure! What do you need?"     I analyze problems methodically

                              and compute exact answers."

```

## The Reasoning Challenge

### Why Reasoning is Hard for LLMs

LLMs are trained on text prediction, not explicit reasoning:

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  LLM Training                         β”‚

β”‚  "Predict next word in text"         β”‚

β”‚                                       β”‚

β”‚  NOT explicitly trained for:         β”‚

β”‚  β€’ Step-by-step logic                β”‚

β”‚  β€’ Arithmetic computation            β”‚

β”‚  β€’ Tracking multiple variables       β”‚

β”‚  β€’ Systematic problem decomposition  β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

However, they can learn reasoning patterns from training data and be guided by system prompts.

## Reasoning Through System Prompts

### Configuration Pattern

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  System Prompt Components              β”‚

β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

β”‚  1. Role: "Expert reasoner"            β”‚

β”‚  2. Task: "Analyze and solve problems" β”‚

β”‚  3. Method: "Compute exact answers"    β”‚

β”‚  4. Output: "Single numeric value"     β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

         ↓

   Reasoning Behavior

```

### Types of Reasoning Tasks

**Quantitative Reasoning (this example):**
```

Problem β†’ Count entities β†’ Calculate β†’ Convert units β†’ Answer

```

**Logical Reasoning:**
```

Premises β†’ Apply rules β†’ Deduce conclusions β†’ Answer

```

**Analytical Reasoning:**
```

Data β†’ Identify patterns β†’ Form hypothesis β†’ Conclude

```

## How LLMs "Reason"

### Pattern Matching vs. True Reasoning

LLMs don't reason like humans, but they can:

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  What LLMs Actually Do                      β”‚

β”‚                                             β”‚

β”‚  1. Pattern Recognition                     β”‚

β”‚     "This looks like a counting problem"    β”‚

β”‚                                             β”‚

β”‚  2. Template Application                    β”‚

β”‚     "Similar problems follow this pattern"  β”‚

β”‚                                             β”‚

β”‚  3. Statistical Inference                   β”‚

β”‚     "These numbers likely combine this way" β”‚

β”‚                                             β”‚

β”‚  4. Learned Procedures                      β”‚

β”‚     "I've seen this type of calculation"    β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

### The Reasoning Process

```

Input: Complex Word Problem

         ↓

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

    β”‚   Parse    β”‚  Identify entities and relationships

    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

         ↓

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

    β”‚  Decompose β”‚  Break into sub-problems

    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

         ↓

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

    β”‚  Calculate β”‚  Apply arithmetic operations

    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

         ↓

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

    β”‚  Synthesizeβ”‚  Combine results

    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

         ↓

     Final Answer

```

## Problem Complexity Hierarchy

### Levels of Reasoning Difficulty

```

Easy                                        Hard

β”‚                                             β”‚

β”‚  Simple    Multi-step   Nested    Implicit β”‚

β”‚  Arithmetic  Logic    Conditions  Reasoningβ”‚

β”‚                                             β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜



Examples:

Easy:    "What is 5 + 3?"

Medium:  "If 3 apples cost $2 each, what's the total?"

Hard:    "Count family members with complex relationships"

```

### This Example's Complexity

The potato problem is **highly complex**:

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  Complexity Factors                     β”‚

β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

β”‚  βœ“ Multiple entities (15+ people)      β”‚

β”‚  βœ“ Relationship reasoning (family tree)β”‚

β”‚  βœ“ Conditional logic (if married then..)β”‚

β”‚  βœ“ Negative conditions (deceased people)β”‚

β”‚  βœ“ Special cases (dietary restrictions)β”‚

β”‚  βœ“ Multiple calculations                β”‚

β”‚  βœ“ Unit conversions                     β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

## Limitations of Pure LLM Reasoning

### Why This Approach Has Issues

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  Problem: No External Tools        β”‚

β”‚                                    β”‚

β”‚  LLM must hold everything in       β”‚

β”‚  "mental" context:                 β”‚

β”‚  β€’ All entity counts               β”‚

β”‚  β€’ Intermediate calculations       β”‚

β”‚  β€’ Conversion factors              β”‚

β”‚  β€’ Final arithmetic                β”‚

β”‚                                    β”‚

β”‚  Result: Prone to errors           β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

### Common Failure Modes

**1. Counting Errors:**
```

Problem: "Count 15 people with complex relationships"

LLM: "14" or "16" (off by one)

```

**2. Arithmetic Mistakes:**
```

Problem: "13 adults Γ— 1.5 + 3 kids Γ— 0.5"

LLM: May get intermediate steps wrong

```

**3. Lost Context:**
```

Problem: Multi-step with many facts

LLM: Forgets earlier information

```

## Improving Reasoning: Evolution Path

### Level 1: Pure Prompting (This Example)
```

User β†’ LLM β†’ Answer

       ↑

   System Prompt

```

**Limitations:**
- All reasoning internal to LLM
- No verification
- No tools
- Hidden process

### Level 2: Chain-of-Thought
```

User β†’ LLM β†’ Show Work β†’ Answer

       ↑

   "Explain your reasoning"

```

**Improvements:**
- Visible reasoning steps
- Can catch some errors
- Still no tools

### Level 3: Tool-Augmented (simple-agent)
```

User β†’ LLM ⟷ Tools β†’ Answer

       ↑    (Calculator)

   System Prompt

```

**Improvements:**
- External computation
- Reduced errors
- Verifiable steps

### Level 4: ReAct Pattern (react-agent)
```

User β†’ LLM β†’ Think β†’ Act β†’ Observe

       ↑      ↓      ↓      ↓

   System  Reason  Tool   Result

   Prompt         Use

       ↑           ↓       ↓

       └───────────Iterateβ”€β”€β”˜

```

**Best approach:**
- Explicit reasoning loop
- Tool use at each step
- Self-correction possible

## System Prompt Design for Reasoning

### Key Elements

**1. Role Definition:**
```

"You are an expert logical and quantitative reasoner"

```
Sets the mental framework.

**2. Task Specification:**
```

"Analyze real-world word problems involving..."

```
Defines the problem domain.

**3. Output Format:**
```

"Return the correct final number as a single value"

```
Controls response structure.

### Design Patterns

**Pattern A: Direct Answer (This Example)**
```

Prompt: [Problem]

Output: [Number]

```
Pros: Concise, fast
Cons: No insight into reasoning

**Pattern B: Show Work**
```

Prompt: [Problem] "Show your steps"

Output: Step 1: ... Step 2: ... Answer: [Number]

```
Pros: Transparent, debuggable
Cons: Longer, may still have errors

**Pattern C: Self-Verification**
```

Prompt: [Problem] "Solve, then verify"

Output: Solution + Verification + Final Answer

```
Pros: More reliable
Cons: Slower, uses more tokens

## Real-World Applications

### Use Cases for Reasoning Agents

**1. Data Analysis:**
```

Input: Dataset summary

Task: Compute statistics, identify trends

Output: Numerical insights

```

**2. Planning:**
```

Input: Goal + constraints

Task: Reason about optimal sequence

Output: Action plan

```

**3. Decision Support:**
```

Input: Options + criteria

Task: Evaluate and compare

Output: Recommended choice

```

**4. Problem Solving:**
```

Input: Complex scenario

Task: Break down and solve

Output: Solution

```

## Comparison: Different Agent Types

```

                  Reasoning  Tools  Memory  Multi-turn

                  ─────────  ─────  ──────  ──────────

intro.js              βœ—        βœ—      βœ—        βœ—

translation.js        ~        βœ—      βœ—        βœ—

think.js (here)       βœ“        βœ—      βœ—        βœ—

simple-agent.js       βœ“        βœ“      βœ—        ~

memory-agent.js       βœ“        βœ“      βœ“        βœ“

react-agent.js        βœ“βœ“       βœ“      ~        βœ“

```

Legend:
- βœ— = Not present
- ~ = Limited/implicit
- βœ“ = Present
- βœ“βœ“ = Advanced/explicit

## Key Takeaways

1. **System prompts enable reasoning**: Proper configuration transforms an LLM into a reasoning agent
2. **Limitations exist**: Pure LLM reasoning is prone to errors on complex problems
3. **Tools help**: External computation (calculators, etc.) improves accuracy
4. **Iteration matters**: Multi-step reasoning patterns (like ReAct) work better
5. **Transparency is valuable**: Seeing the reasoning process helps debug and verify

## Next Steps

After understanding basic reasoning:
- **Add tools**: Let the agent use calculators, databases, APIs
- **Implement verification**: Check answers, retry on errors
- **Use chain-of-thought**: Make reasoning explicit
- **Apply ReAct pattern**: Combine reasoning and tool use systematically

This example is the foundation for more sophisticated agent architectures that combine reasoning with external capabilities.