# Chain of Thought

In this example, we use flow control, prefill and constrained generation to guide the model to reason about a problem for a precise number of steps before trying to provide an answer.

chain-of-thought.js

```
// present the model with a word problem
prompt(
"Roger has 5 tennis balls. " +
"He buys 2 more cans of tennis balls. " +
"Each can has 3 tennis balls. " +
"How many tennis balls does he have now?\n",
{ role: 'user' }
);
// prefill the model's to start the reasoning process
prompt("\nLet's think though this step by step: \n", { role: 'assistant' } );
// prompt to perform exactly 4 reasoning steps
let steps = 4;
for (let i = 0; i < steps; i++) {
prompt(`Step ${i + 1}. `, { role: 'assistant' });
// limit the length of each reasoning step to 100 tokens
gen({ limit: 100, stop_at: "\n", role: 'assistant' });
}
// prompt to provide the final answer, limit the output to numbers only.
prompt(`\n\nTherefore, the answer is: `, { role: 'assistant' });
gen({ regex: "[0-9]+", role: 'assistant' });
```

output.txt

```
Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
Let's think though this step by step:
Step 1. Roger already has 5 tennis balls.
Step 2. He buys 2 more cans of tennis balls, each containing 3 tennis balls.
Step 3. To find the total number of new tennis balls he bought, we multiply the number of cans (2) by the number of tennis balls in each can (3), which is 2 * 3 = 6.
Step 4. Now, to find the total number of tennis balls Roger has now, we add the initial number of tennis balls (5) to the new tennis balls he bought (6), which is 5 + 6 = 11.
Therefore, the answer is: 11
```