DeepSeek R1 API Tutorial: How to Use the Best Free Reasoning Model
DeepSeek R1 rivals o3 on math and coding at a fraction of the cost — or free via FreeLLMKeys. Full API tutorial with Python examples and benchmark results.
What Makes DeepSeek R1 Special
DeepSeek R1 is a reasoning model — like OpenAI's o3, it thinks through problems step by step before answering. What makes R1 remarkable is that it matches or exceeds o3's performance on math and coding benchmarks, has an MIT open-source license (meaning you can run it locally), and costs a fraction of what OpenAI charges at scale.
On AIME 2024 (a high school math competition used to benchmark AI), R1 scored 79.8% — compared to o3's 96.7% at full compute and o1's 74.4%. For most developers, R1 is the better cost-performance choice for reasoning tasks.
Quick Start — Make Your First R1 API Call
from openai import OpenAI
client = OpenAI(
base_url="https://aiapiv2.pekpik.com/v1",
api_key="sk-your-freellmkeys-key"
)
response = client.chat.completions.create(
model="deepseek-r1",
messages=[
{
"role": "user",
"content": "What is the probability that in a group of 23 people, at least two share a birthday? Show your reasoning."
}
]
)
print(response.choices[0].message.content)
R1 will show its reasoning process (called "thinking") before giving the final answer. This transparency is a major advantage over black-box models — you can see exactly where the model's reasoning went right or wrong.
R1 vs R1-Zero vs DeepSeek V3 — What Is the Difference?
| Model | Type | Best For | Speed |
|---|---|---|---|
| DeepSeek R1 | Reasoning (RLHF) | Math, complex reasoning, code debugging | Slower |
| DeepSeek R1-Zero | Reasoning (pure RL) | Research use, raw reasoning without alignment | Slower |
| DeepSeek V3 | General purpose | Code generation, writing, general tasks | Fast |
For most developer tasks: use V3 for generation and R1 for reasoning/debugging.
Using R1 for Algorithm Problem Solving
algorithm_prompt = """
Solve this algorithm problem step by step:
Given an array of integers, find the length of the longest subarray
with sum equal to zero.
Example: [15, -2, 2, -8, 1, 7, 10, 23] → answer is 5 ([-2, 2, -8, 1, 7])
1. Explain your approach
2. Analyze the time and space complexity
3. Write the Python implementation
4. Walk through the example to verify
"""
response = client.chat.completions.create(
model="deepseek-r1",
messages=[{"role": "user", "content": algorithm_prompt}]
)
print(response.choices[0].message.content)
R1 is significantly better than V3 for this type of problem — it constructs a solution through deliberate reasoning rather than pattern-matching from training data.
Using R1 for Complex Bug Debugging
buggy_code = """
def find_duplicates(nums):
seen = set()
duplicates = []
for i in range(len(nums)):
for j in range(i, len(nums)): # Bug here
if nums[i] == nums[j]:
if nums[i] not in seen:
duplicates.append(nums[i])
seen.add(nums[i])
return duplicates
# Expected: [2, 3] for input [1, 2, 3, 2, 3, 4]
# Actual result is wrong — why?
"""
debug_response = client.chat.completions.create(
model="deepseek-r1",
messages=[{
"role": "user",
"content": f"Find and explain all bugs in this Python function, then write the corrected version:\n{buggy_code}"
}]
)
print(debug_response.choices[0].message.content)
Streaming R1 Responses
R1's thinking process can be long. Use streaming to show progress to users:
stream = client.chat.completions.create(
model="deepseek-r1",
messages=[{"role": "user", "content": "Prove that there are infinitely many prime numbers."}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end='', flush=True)
When to Use R1 vs GPT-4o vs Claude
| Task | Best Model |
|---|---|
| Math proofs and derivations | DeepSeek R1 |
| Algorithm design (medium/hard) | DeepSeek R1 or o3 |
| Complex bug analysis | DeepSeek R1 or Claude Opus |
| Fast code generation | DeepSeek V3 or GPT-4o |
| Long document analysis | Claude Opus 4 |
| Writing and documentation | Claude Sonnet 4 |
| Speed-sensitive applications | Gemini 2.5 Flash |
Cost Comparison
At official API pricing, DeepSeek R1 costs approximately $0.55 per million input tokens — compared to $15 per million for o3. That is a 27x cost difference for comparable reasoning quality on most tasks. Via FreeLLMKeys, both are free for development and prototyping.
Grab a key from FreeLLMKeys.com and run the examples above — you will see why R1 has become the default reasoning model for cost-conscious developers.