What Does 1M Context Mean
DeepSeek V4's 1M token context window is roughly equivalent to:
- 700,000 Chinese characters—the length of the entire 'Three-Body Problem' trilogy
- 1,000 pages of PDF documents
- 100,000 lines of code
- Hundreds of rounds of conversation history
But the larger the window, the more strategy is needed—you can't just stuff data in blindly, or token costs and latency will spiral out of control.
Message Truncation Strategy
For multi-turn conversations, the most common need is to keep the most recent N turns and truncate earlier messages:
def trim_conversation(messages, max_turns=20):
"""Keep system prompt + recent max_turns turns of conversation"""
system_msgs = [m for m in messages if m['role'] == 'system']
chat_msgs = [m for m in messages if m['role'] != 'system']
# Keep the most recent max_turns turns (each turn = user + assistant)
trimmed = chat_msgs[-(max_turns * 2):]
return system_msgs + trimmed
# Usage
messages = [{"role": "system", "content": "You are a programming assistant"}]
# ... after multiple turns
messages = trim_conversation(messages, max_turns=15)
response = client.chat.completions.create(
model='deepseek-v4-flash',
messages=messages
)Summary Compression Strategy
A better approach is to compress historical messages into a summary rather than discarding them outright:
async def compress_history(messages, threshold_turns=10):
"""When conversation exceeds threshold_turns, compress early messages into a summary"""
system_msgs = [m for m in messages if m['role'] == 'system']
chat_msgs = [m for m in messages if m['role'] != 'system']
if len(chat_msgs) <= threshold_turns * 2:
return messages # No compression needed
# Early messages to text
early = chat_msgs[:-(threshold_turns * 2)]
recent = chat_msgs[-(threshold_turns * 2):]
history_text = '\n'.join([f"{m['role']}: {m['content'][:200]}" for m in early])
# Generate summary
summary_response = await client.chat.completions.create(
model='deepseek-v4-flash',
messages=[{
"role": "user",
"content": f"Summarize the key information from the following conversation in one Chinese paragraph:\n{history_text}"
}]
)
summary = summary_response.choices[0].message.content
return system_msgs + [
{"role": "system", "content": f"[Historical conversation summary] {summary}"}
] + recentLong Document Chunking
For extremely long documents (like a whole book), chunking is the most practical method:
def chunk_document(text, chunk_size=30000):
"""Split long text into chunks suitable for the context window"""
chunks = []
for i in range(0, len(text), chunk_size):
chunks.append(text[i:i+chunk_size])
return chunks
async def analyze_long_doc(text, question):
chunks = chunk_document(text)
findings = []
# Phase 1: Analyze each chunk
for i, chunk in enumerate(chunks):
response = await client.chat.completions.create(
model='deepseek-v4-flash',
messages=[{
"role": "system",
"content": "Extract key information from the text chunk relevant to the question. If none, answer 'None'."
}, {
"role": "user",
"content": f"Question: {question}\nText chunk [{i+1}/{len(chunks)}]: {chunk}"
}]
)
result = response.choices[0].message.content
if result != 'None':
findings.append(f"[Chunk {i+1}] {result}")
# Phase 2: Aggregate analysis
context = '\n\n'.join(findings)
final = await client.chat.completions.create(
model='deepseek-v4-pro', # Use Pro for aggregation to ensure quality
messages=[{
"role": "user",
"content": f"Answer the following question based on the analysis results: {question}\n\n{context}"
}]
)
return final.choices[0].message.contentToken Monitoring
# Check token usage after each request
response = client.chat.completions.create(...)
usage = response.usage
print(f"Input: {usage.prompt_tokens} tokens")
print(f"Output: {usage.completion_tokens} tokens")
print(f"Total: {usage.total_tokens} tokens")
# Estimate context usage rate
if usage.prompt_tokens > 800000:
print("Warning: Context usage exceeds 80%, consider enabling compression!")
Best Practices Summary
- Keep daily conversations within 20 turns using truncation strategy
- Use summary compression when history needs to be preserved
- For very long documents, use the two-phase method of chunking + aggregation
- Always monitor token usage and handle proactively when approaching the limit