DEV Community

shashank ms
shashank ms

Posted on

Deep Reasoning with LLM: A Comprehensive Guide

We are building a Deep Reasoning Research Agent that decomposes complex technical questions, critiques its own logic, and delivers a structured verdict. This helps engineering teams evaluate architectural decisions without shallow, generic answers.

What you'll need

You need Python 3.10 or newer, the OpenAI SDK, and an Oxlo.ai API key. Install the SDK and grab your key from the portal.

pip install openai

Step 1: Configure the Oxlo.ai client

Point the OpenAI SDK at Oxlo.ai. I use kimi-k2.6 here because its 131K context window and advanced reasoning handle long agent traces without losing coherence.

from openai import OpenAI
import json

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

def ask_model(messages, temperature=0.2):
    response = client.chat.completions.create(
        model="kimi-k2.6",
        messages=messages,
        temperature=temperature,
    )
    return response.choices[0].message.content

Step 2: Define the reasoning system prompt

The system prompt forces the model to expose its reasoning chain, assign confidence, and flag uncertainty. This is the core of the agent.

SYSTEM_PROMPT = """You are a Deep Reasoning Research Agent. Your job is to analyze complex technical questions rigorously.

Follow these rules:
1. Break the problem into sub-questions.
2. Reason step by step for each sub-question. Show your work.
3. Assign a confidence score (1-10) to each claim.
4. If you detect weak reasoning or missing data, explicitly state the gap.
5. Before concluding, critique your own argument and correct any flaws.
6. Output your final answer as valid JSON with keys: verdict, reasoning_steps, confidence, caveats.

Be precise. Avoid speculation beyond the evidence you can derive from first principles and general technical knowledge."""

Step 3: Decompose the question

First, ask the model to split the user question into discrete sub-questions. This keeps each reasoning chunk focused and verifiable.

def decompose_question(user_question):
    prompt = (
        "Decompose the following complex question into 3 to 5 specific sub-questions. "
        "Return only a JSON list of strings.\n\nQuestion: " + user_question
    )
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages)
    # Strip markdown fences if present
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

# Example
sub_questions = decompose_question(
    "Should our team adopt a microservices architecture for a real-time analytics platform handling 1M events per second?"
)
print(sub_questions)

Step 4: Reason through each sub-question

Run each sub-question through the model individually. Because Oxlo.ai charges a flat rate per request, running multiple short reasoning turns does not scale in cost with prompt length the way token-based billing does. You can iterate without surprise bills.

def reason_sub_question(sub_q):
    prompt = f"Sub-question: {sub_q}\n\nThink step by step. Then output your analysis as JSON with keys: sub_question, reasoning, confidence, gaps."
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages, temperature=0.3)
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

def collect_reasoning(sub_questions):
    analyses = []
    for sq in sub_questions:
        analyses.append(reason_sub_question(sq))
    return analyses

analyses = collect_reasoning(sub_questions)
for a in analyses:
    print(a["sub_question"], "Confidence:", a["confidence"])

Step 5: Reflect and critique

Feed the collected analyses back to the model and ask it to find contradictions or weak links. This reflection layer is what turns a simple chain-of-thought into deep reasoning.

def reflect_and_refine(analyses):
    payload = json.dumps(analyses, indent=2)
    prompt = (
        "You have the following sub-analyses. Critique them as a whole. "
        "Identify contradictions, overconfident claims, and missing evidence. "
        "Then produce a refined synthesis as JSON with keys: critique, refined_reasoning, updated_confidence, remaining_uncertainties.\n\n"
        + payload
    )
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages, temperature=0.2)
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

refined = reflect_and_refine(analyses)
print("Critique:", refined["critique"])

Step 6: Synthesize the final report

Generate a structured verdict. The model condenses the refined reasoning into a decision the team can act on.

def synthesize_verdict(user_question, refined):
    prompt = (
        f"Original question: {user_question}\n\n"
        f"Refined reasoning: {json.dumps(refined, indent=2)}\n\n"
        "Deliver a final structured verdict as JSON with keys: "
        "recommendation (one sentence), rationale (list of strings), overall_confidence (1-10), action_items (list of strings)."
    )
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages, temperature=0.2)
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

verdict = synthesize_verdict(
    "Should our team adopt a microservices architecture for a real-time analytics platform handling 1M events per second?",
    refined
)
print(json.dumps(verdict, indent=2))

Run it

Here is the complete script wired together. Drop in your Oxlo.ai key and run it against any hard technical question.

from openai import OpenAI
import json

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

SYSTEM_PROMPT = """You are a Deep Reasoning Research Agent. Your job is to analyze complex technical questions rigorously.

Follow these rules:
1. Break the problem into sub-questions.
2. Reason step by step for each sub-question. Show your work.
3. Assign a confidence score (1-10) to each claim.
4. If you detect weak reasoning or missing data, explicitly state the gap.
5. Before concluding, critique your own argument and correct any flaws.
6. Output your final answer as valid JSON with keys: verdict, reasoning_steps, confidence, caveats.

Be precise. Avoid speculation beyond the evidence you can derive from first principles and general technical knowledge."""

def ask_model(messages, temperature=0.2):
    response = client.chat.completions.create(
        model="kimi-k2.6",
        messages=messages,
        temperature=temperature,
    )
    return response.choices[0].message.content

def decompose_question(user_question):
    prompt = (
        "Decompose the following complex question into 3 to 5 specific sub-questions. "
        "Return only a JSON list of strings.\n\nQuestion: " + user_question
    )
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages)
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

def reason_sub_question(sub_q):
    prompt = f"Sub-question: {sub_q}\n\nThink step by step. Then output your analysis as JSON with keys: sub_question, reasoning, confidence, gaps."
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages, temperature=0.3)
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

def collect_reasoning(sub_questions):
    return [reason_sub_question(sq) for sq in sub_questions]

def reflect_and_refine(analyses):
    payload = json.dumps(analyses, indent=2)
    prompt = (
        "You have the following sub-analyses. Critique them as a whole. "
        "Identify contradictions, overconfident claims, and missing evidence. "
        "Then produce a refined synthesis as JSON with keys: critique, refined_reasoning, updated_confidence, remaining_uncertainties.\n\n"
        + payload
    )
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages, temperature=0.2)
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

def synthesize_verdict(user_question, refined):
    prompt = (
        f"Original question: {user_question}\n\n"
        f"Refined reasoning: {json.dumps(refined, indent=2)}\n\n"
        "Deliver a final structured verdict as JSON with keys: "
        "recommendation (one sentence), rationale (list of strings), overall_confidence (1-10), action_items (list of strings)."
    )
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": prompt},
    ]
    raw = ask_model(messages, temperature=0.2)
    clean = raw.replace("

```json", "").replace("```

", "").strip()
    return json.loads(clean)

if __name__ == "__main__":
    question = (
        "Should our team adopt a microservices architecture for a real-time analytics platform handling 1M events per second?"
    )

    print("Decomposing...")
    subs = decompose_question(question)

    print("Reasoning through sub-questions...")
    analyses = collect_reasoning(subs)

    print("Reflecting...")
    refined = reflect_and_refine(analyses)

    print("Synthesizing verdict...")
    final = synthesize_verdict(question, refined)

    print("\n=== FINAL VERDICT ===")
    print(json.dumps(final, indent=2))

Example output:

Decomposing...
Reasoning through sub-questions...
Reflecting...
Synthesizing verdict...

=== FINAL VERDICT ===
{
  "recommendation": "Adopt a modular monolith first, with clear service boundaries to enable future extraction.",
  "rationale": [
    "1M events per second demands low latency inter-process communication, which microservices networks often degrade.",
    "Operational overhead of distributed tracing and deployment complexity outweighs team velocity gains at current scale.",
    "A modular monolith preserves code locality while allowing horizontal scaling of the ingestion layer."
  ],
  "overall_confidence": 8,
  "action_items": [
    "Benchmark in-process event bus latency against gRPC in staging.",
    "Define bounded contexts now so extraction later is mechanical.",
    "Revisit the decision after 6 months of load growth."
  ]
}

Wrap-up

You now have a working deep reasoning agent that decomposes, critiques, and synthesizes. Swap in deepseek-v3.2 if you want to test against a coding-heavy reasoning style, or try qwen-3-32b for multilingual technical docs. Because Oxlo.ai uses request-based pricing, iterating with long system prompts and multi-turn reflection loops does not inflate your bill the way token-based providers do. Check the details at https://oxlo.ai/pricing.

Two concrete next steps: wire the agent into a Slack bot so teams can query it from channels, or add a retrieval step with the embeddings endpoint to ground its reasoning in your internal architecture docs.

Top comments (0)