We are building a chain-of-thought reasoning agent that solves multi-step logic and math problems by explicitly writing out its reasoning before committing to an answer. This pattern is essential for any production system where silent reasoning errors are expensive, from billing audit tools to medical dosage checkers.
What you'll need
- An Oxlo.ai API key from https://portal.oxlo.ai
- Python 3.10 or newer
- The OpenAI SDK:
pip install openai
Step 1: Configure the Oxlo.ai client
We will use the OpenAI SDK as a drop-in replacement pointed at Oxlo.ai. In production you should load the key from an environment variable, but a hardcoded string is fine for local testing.
from openai import OpenAI
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")
Step 2: Write the chain-of-thought system prompt
The prompt is the product. We force the model to separate reasoning from the final answer so we can log and audit the trace. I use a strict XML-like structure because it parses reliably with simple regex.
SYSTEM_PROMPT = """You are a reasoning engine. When given a problem, you must think step by step inside <thinking> tags. Show all calculations, consider edge cases, and verify intermediate results. Only after you close the </thinking> tag, provide the final answer inside <answer> tags. Do not skip steps or guess."""
Step 3: Build the solver function
This helper sends the user question to Oxlo.ai and returns the raw response. I use kimi-k2.6 here because its advanced reasoning capabilities handle long context well, but deepseek-v3.2 or qwen-3-32b work too.
def solve_with_cot(problem: str, model: str = "kimi-k2.6") -> str:
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": problem},
],
temperature=0.2,
max_tokens=2048,
)
return response.choices[0].message.content
Step 4: Parse and validate the reasoning trace
Raw text is fine for demos, but production code should extract the thinking and answer blocks separately. This lets us store the trace for later audit without regex nightmares.
from typing import Tuple
import re
def parse_cot(raw: str) -> Tuple[str, str]:
thinking = re.search(r"<thinking>(.*?)</thinking>", raw, re.DOTALL)
answer = re.search(r"<answer>(.*?)</answer>", raw, re.DOTALL)
think_text = thinking.group(1).strip() if thinking else "No reasoning trace found."
answer_text = answer.group(1).strip() if answer else "No answer block found."
return think_text, answer_text
Step 5: Test with a deliberately tricky problem
I use a classic rate problem with a twist: it requires unit conversion and checking for leftover inventory. These trip up models that jump straight to the answer.
PROBLEM = (
"A factory produces 144 widgets every 6 hours. "
"Each shipping crate holds 25 widgets. "
"After 3 days of continuous production, how many full crates can be shipped, "
"and how many widgets remain? Assume production runs 24 hours per day."
)
raw_output = solve_with_cot(PROBLEM)
thinking, answer = parse_cot(raw_output)
print("=== REASONING TRACE ===")
print(thinking)
print("\n=== FINAL ANSWER ===")
print(answer)
Run it
When I run this against Oxlo.ai, the agent returns a complete trace. Here is actual output from kimi-k2.6 (lightly trimmed for brevity):
=== REASONING TRACE ===
First, determine total production time: 3 days × 24 hours/day = 72 hours.
Next, find production cycles: 72 hours ÷ 6 hours per cycle = 12 cycles.
Total widgets produced: 12 cycles × 144 widgets = 1,728 widgets.
Now divide by crate capacity: 1,728 ÷ 25 = 69.12.
Full crates: floor(69.12) = 69 crates.
Remaining widgets: 1,728 - (69 × 25) = 1,728 - 1,725 = 3 widgets.
Verify: 69 × 25 + 3 = 1,725 + 3 = 1,728. Check passed.
=== FINAL ANSWER ===
69 full crates can be shipped, with 3 widgets remaining.
Wrap-up and next steps
Two concrete next steps. First, wire the reasoning trace into your logging pipeline so you can surface it in a UI when users challenge an answer. Second, try swapping in deepseek-v3.2 for harder coding puzzles, or switch to qwen-3-32b if you need multilingual reasoning. Because Oxlo.ai uses request-based pricing, long reasoning traces do not inflate your bill the way token-based providers do, which makes extensive chain-of-thought workloads practical at scale. Check the details at https://oxlo.ai/pricing.
Top comments (0)