TechLead
Lección 4 de 8

Prompting de Cadena de Pensamiento

Técnicas de razonamiento paso a paso para mejorar la resolución de problemas complejos

¿Qué es Chain of Thought (CoT)?

El prompting Chain of Thought anima al modelo a descomponer problemas complejos en pasos de razonamiento intermedios. En lugar de saltar directamente a la respuesta, el modelo “piensa” el problema paso a paso, mejorando drásticamente la precisión en tareas complejas.

Esta técnica fue introducida por investigadores de Google en 2022 y se ha convertido en uno de los métodos más efectivos para mejorar el rendimiento de los LLM en tareas de razonamiento.

🧠 Por qué funciona CoT

  • Divide la complejidad: Descompone problemas difíciles en pasos manejables
  • Reduce errores: Cada paso puede verificarse de forma independiente
  • Revela el razonamiento: Puedes ver cómo llegó el modelo a su conclusión
  • Imita el pensamiento humano: Refleja cómo los expertos resuelven problemas complejos

Prompting estándar vs Chain of Thought

En el prompting estándar, el modelo intenta responder directamente, lo que puede llevar a errores en problemas con varios pasos. Con CoT, el modelo muestra su razonamiento intermedio, lo que reduce significativamente los errores.

// ❌ Standard Prompting (direct answer)
"A store has 23 apples. They sell 17 and receive a shipment 
of 12 more. How many apples do they have?"

Answer: 18 (Model may get this wrong by rushing)

// ✅ Chain of Thought Prompting
"A store has 23 apples. They sell 17 and receive a shipment 
of 12 more. How many apples do they have?

Let's think through this step by step:"

Response:
"Step 1: Start with 23 apples
Step 2: After selling 17: 23 - 17 = 6 apples
Step 3: After receiving 12 more: 6 + 12 = 18 apples

The store has 18 apples."

La frase mágica

Simplemente añadir "Let's think step by step" puede mejorar drásticamente los resultados. Esto se llama CoT zero-shot.

// Zero-Shot Chain of Thought
"[Complex problem]

Let's think step by step."

// Alternative trigger phrases:
"Let's work through this systematically."
"Let's break this down:"
"Let's solve this step by step:"
"Think through each step carefully:"
"First, let's understand the problem, then solve it:"

// Example: Debugging
"My React component re-renders infinitely when I add 
this useEffect. Why is this happening?

```jsx
useEffect(() => {
  setData(fetchData());
}, [data]);
```

Let's think through this step by step."

// Model will analyze:
// 1. What triggers useEffect
// 2. What happens inside useEffect
// 3. How that affects the dependency
// 4. Why this creates a loop

Chain of Thought few-shot

Proporciona ejemplos que demuestren el proceso de razonamiento que quieres que el modelo siga.

// Few-Shot CoT Example
"Solve these coding problems by thinking step by step.

Problem: Is the array [3, 1, 4, 1, 5] sorted in ascending order?
Thinking:
1. Check if 3 <= 1: No, 3 > 1
2. The first comparison failed
Answer: No, the array is not sorted (3 > 1)

Problem: Is the array [1, 2, 3, 4, 5] sorted in ascending order?
Thinking:
1. Check if 1 <= 2: Yes
2. Check if 2 <= 3: Yes
3. Check if 3 <= 4: Yes
4. Check if 4 <= 5: Yes
5. All comparisons passed
Answer: Yes, the array is sorted

Problem: Is the array [1, 3, 2, 4] sorted in ascending order?
Thinking:"

// Model will follow the same reasoning pattern

CoT para análisis de código

Pedir al modelo que analice código paso a paso resulta en evaluaciones más precisas de complejidad temporal, ya que identifica cada operación por separado antes de calcular el total.

// Analyzing code complexity
"Analyze the time complexity of this function.
Think through each operation step by step.

function findDuplicates(arr) {
  const seen = new Set();
  const duplicates = [];
  
  for (const item of arr) {
    if (seen.has(item)) {
      duplicates.push(item);
    } else {
      seen.add(item);
    }
  }
  
  return duplicates;
}

Analysis:
Step 1: Identify the loops
- One for...of loop iterating through arr (n iterations)

Step 2: Analyze operations inside the loop
- Set.has() - O(1) average
- Set.add() - O(1) average
- Array.push() - O(1) amortized

Step 3: Calculate total complexity
- Loop runs n times
- Each iteration does O(1) work
- Total: O(n)

Time Complexity: O(n)
Space Complexity: O(n) for the Set and duplicates array"

CoT para decisiones de arquitectura

Chain of Thought es especialmente útil para decisiones de diseño donde hay múltiples factores a considerar. El modelo evalúa cada aspecto sistemáticamente antes de dar una recomendación fundamentada.

// Making architecture decisions
"I need to choose between REST and GraphQL for my new API.
Let's think through this systematically.

Context:
- E-commerce app with mobile and web clients
- Complex product data with many relationships
- Need real-time inventory updates
- Team has REST experience

Analysis:

Step 1: Data Fetching Patterns
- Products have categories, reviews, inventory, variants
- Mobile app needs minimal data (bandwidth concerns)
- Web app needs full details
→ GraphQL advantage: clients request exactly what they need

Step 2: Team Experience
- Team knows REST well
- GraphQL has learning curve
→ REST advantage: faster initial development

Step 3: Real-time Requirements
- Need live inventory updates
- REST: Would need WebSocket layer
- GraphQL: Built-in subscriptions
→ GraphQL advantage: native subscription support

Step 4: Caching
- REST: HTTP caching is straightforward
- GraphQL: More complex caching strategies needed
→ REST advantage: simpler caching

Recommendation:
Start with REST for core CRUD operations (faster development),
add GraphQL for complex product queries (mobile optimization),
use WebSocket for real-time inventory updates."

CoT para depuración

Al depurar código, CoT guía al modelo a examinar cada línea y operación individualmente, identificando errores que podrían pasar desapercibidos en un análisis superficial.

// Debugging with Chain of Thought
"Debug this code. Think through each potential issue:

async function getUserPosts(userId) {
  const user = await fetch(`/api/users/${userId}`);
  const posts = await fetch(`/api/posts?userId=${user.id}`);
  return posts;
}

Step-by-step analysis:

Step 1: First fetch call
- fetch() returns a Response object, not the data
- Missing: .json() or .text() to parse response
→ Bug: 'user' is a Response, not a user object

Step 2: Second fetch call
- Same issue: fetch returns Response
- Also using 'user.id' but 'user' is a Response object
→ Bug: 'user.id' is undefined

Step 3: Return value
- 'posts' is a Response object
- Caller likely expects an array
→ Bug: returning Response instead of data

Fixed code:
async function getUserPosts(userId) {
  const userResponse = await fetch(`/api/users/${userId}`);
  const user = await userResponse.json();
  
  const postsResponse = await fetch(`/api/posts?userId=${user.id}`);
  const posts = await postsResponse.json();
  
  return posts;
}"

Autoconsistencia con CoT

Genera múltiples rutas de razonamiento y toma la respuesta mayoritaria para mayor precisión.

// Self-Consistency: Multiple reasoning paths
"Solve this problem using three different approaches, 
then determine which answer is most likely correct.

Problem: A function receives an array and should return 
true if any two numbers sum to 10. What's the most 
efficient time complexity possible?

Approach 1 (Brute Force Analysis):
- Check every pair: O(n²)
- Can we do better? Yes, with a hash set

Approach 2 (Hash Set Analysis):
- For each number x, check if (10-x) exists
- Set lookup is O(1)
- One pass through array: O(n)

Approach 3 (Sorted Array Analysis):
- Sort: O(n log n)
- Two pointers: O(n)
- Total: O(n log n)

Conclusion: 
Approaches agree that O(n) is achievable with hash set.
Answer: O(n) time complexity"

✅ Cuándo usar Chain of Thought

  • • Problemas de matemáticas y lógica
  • • Depuración y análisis de código
  • • Decisiones de arquitectura y diseño
  • • Tareas de razonamiento de varios pasos
  • • Cuando necesitas entender el razonamiento
  • • Transformaciones complejas de datos