APIMaster.ai

DeepSeek R1 API Guide — Reasoning Model Access | APIMaster.ai

How to use the DeepSeek R1 API for chain-of-thought reasoning. Python examples, prompt strategies, and how to access DeepSeek R1 via APIMaster.ai without restrictions.

DeepSeek R1 API Guide

DeepSeek R1 is a reasoning-focused model that shows its chain of thought before answering—similar to OpenAI's o-series. It excels at math, science, and multi-step logic problems. This guide covers the R1 API, best practices, and access via APIMaster.

What Makes DeepSeek R1 Different

Unlike standard chat models, R1:

  1. Reasons before answering: it outputs a <think>...</think> block with step-by-step reasoning
  2. Excels at formal reasoning: math proofs, code verification, logic puzzles
  3. Open weights: the base model is open-source (weights available on HuggingFace)
  4. Competitive performance: matches o1 on many benchmarks at much lower cost

DeepSeek R1 API Quickstart

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_APIMASTER_KEY",
    base_url="https://apimaster.ai/v1",
)

response = client.chat.completions.create(
    model="deepseek-r1",
    messages=[
        {
            "role": "user",
            "content": "Prove that the square root of 2 is irrational.",
        }
    ],
    max_tokens=2048,  # R1 needs more tokens for reasoning
)

print(response.choices[0].message.content)

Understanding R1's Chain-of-Thought Output

R1 returns its reasoning in <think> tags followed by a clean answer:

<think>
To prove √2 is irrational, I'll use proof by contradiction.

Assume √2 = p/q where p, q are integers with no common factors...
[detailed reasoning steps]
...Therefore, we have a contradiction. √2 must be irrational.
</think>

**Proof by contradiction:**

Assume √2 is rational, meaning √2 = p/q where p and q are coprime integers...

To extract just the final answer:

import re

def get_answer(response_text):
    # Remove <think> block
    clean = re.sub(r'<think>.*?</think>', '', response_text, flags=re.DOTALL)
    return clean.strip()

content = response.choices[0].message.content
answer = get_answer(content)
print(answer)

Prompt Strategies for DeepSeek R1

Math and Proofs

response = client.chat.completions.create(
    model="deepseek-r1",
    messages=[
        {
            "role": "user",
            "content": """Solve step by step:
Find all integer solutions to: x² - 5y² = 1

Show your reasoning."""
        }
    ],
    max_tokens=3000,
)

Code Verification

code = """
def merge_sort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    return merge(left, right)
"""

response = client.chat.completions.create(
    model="deepseek-r1",
    messages=[
        {
            "role": "user",
            "content": f"Verify this merge sort implementation is correct:\n\n```python\n{code}\n```\n\nFind any bugs or edge cases."
        }
    ],
)

Multi-Step Logic

response = client.chat.completions.create(
    model="deepseek-r1",
    messages=[
        {
            "role": "user",
            "content": """You have 3 boxes. One contains only apples, one only oranges, one both. All boxes are mislabeled. You can draw one fruit from one box. Which box do you pick and why?"""
        }
    ],
)

DeepSeek R1 vs Other Reasoning Models

Model Strengths Price Range Context
deepseek-r1 Math, science, open weights Very low 64K
o3 (OpenAI) Broad reasoning High 200K
o4-mini Fast reasoning Medium 128K
claude-opus-4-8 Complex analysis High 200K

R1 is typically 5–10× cheaper than o3 for comparable reasoning tasks.

Handling Long Reasoning Outputs

R1 can produce very long outputs—set max_tokens high for complex tasks:

response = client.chat.completions.create(
    model="deepseek-r1",
    messages=[{"role": "user", "content": "Solve this calculus problem: ..."}],
    max_tokens=4096,  # High limit for complex reasoning
)

# Check if output was truncated
if response.choices[0].finish_reason == "length":
    print("Warning: Output truncated—increase max_tokens")

Streaming R1 Responses

For better UX on long reasoning tasks:

with client.chat.completions.stream(
    model="deepseek-r1",
    messages=[{"role": "user", "content": "Explain Gödel's incompleteness theorems."}],
    max_tokens=3000,
) as stream:
    thinking = False
    for text in stream.text_stream:
        if "<think>" in text:
            thinking = True
        if thinking:
            print(text, end="", flush=True)  # Show reasoning
        if "</think>" in text:
            thinking = False

DeepSeek R1 API Pricing

Model Input (per 1M) Output (per 1M)
DeepSeek R1 $0.55 $2.19
DeepSeek V4 (non-reasoning) $0.27 $1.10

See APIMaster marketplace for current rates.

Get DeepSeek R1 API Access

APIMaster provides R1 access globally with no geographic restrictions:

Get started → · DeepSeek API Guide → · DeepSeek Pricing →