# Models and the DeepSeek API

> The DeepSeek models RaptorCode supports, the request settings it sends, and how it handles finish reasons, errors, retries and the context window.

RaptorCode talks to the official DeepSeek API directly, using its OpenAI-format endpoints:

| Endpoint | Used for |
|---|---|
| `POST https://api.deepseek.com/chat/completions` | Chat, streaming and agent tool turns |
| `GET https://api.deepseek.com/models` | The `/model` picker and the startup key check |
| `GET https://api.deepseek.com/user/balance` | `/balance` and the first-run key check |

## Models

| API name | Model | Notes |
|---|---|---|
| `deepseek-v4-pro` | DeepSeek V4 Pro | Default. Text only. |
| `deepseek-flash` | DeepSeek V4.1 Flash | Faster and cheaper; accepts images. |

Both have a 1,048,576-token context window (prompt plus output), up to 393,216 output tokens (384K), tool calls, JSON output and thinking mode with `low`, `high` and `max` effort.

Retired names are mapped before a request is sent: `deepseek-v4-flash`, `deepseek-v4-flash-vision-exp`, `deepseek-chat` and `deepseek-reasoner` all become `deepseek-flash`. Any other id is sent unchanged.

## Request settings

- `thinking`, default `enabled`:
Thinking mode is on by default. `/thinking` switches it off and back on.
- `reasoning effort`, default `max in the REPL, swarm and run; high in chat and planning`:
`/effort` cycles low → high → max; `raptor run` takes `--effort`.
- `max_tokens`, default `131072`:
Every generation allows 128K output tokens, so a large `write_file` call is not cut off mid-argument. Output is billed as generated, so the larger budget costs nothing by itself.
- `stream`, default `on`:
On by default in the REPL, always on for swarm agents and `raptor run`. Streamed requests ask for usage with `stream_options: {"include_usage": true}`.
- `response_format`, default `text`:
`/json` sends `{"type": "json_object"}` and a system instruction asking for JSON, as DeepSeek requires.

`temperature`, `top_p` and penalties are not sent: thinking mode ignores them, and DeepSeek's defaults are what its own agent evaluations use.

## Thinking mode and tool calls

When a request carries tools, DeepSeek requires the reasoning content of every earlier assistant turn to be sent back. RaptorCode keeps it on every assistant message and replays it, and never appends an assistant message of its own making, so typing `continue` after the step limit works.

## Finish reasons

| `finish_reason` | What the agent loop does |
|---|---|
| `stop`, `tool_calls` | Normal end of a step. |
| `length` with text | Keeps the answer and asks the model to continue where it stopped (up to 3 times in a row). |
| `length` with tool calls | Runs the complete calls; a call cut off mid-arguments is not run, and the model is told to send smaller pieces. |
| `insufficient_system_resource`, `aborted` | Records nothing and resends the request (up to 2 times), then reports the interruption. |
| `content_filter` | Returns the answer with a note that DeepSeek's content filter stopped it. |

## Errors and retries

| Status | Handling |
|---|---|
| 400, 422 | Not retried; DeepSeek's message is shown. |
| 401, 403 | Not retried; the REPL offers to re-enter the key without losing history. |
| 402 | Not retried; the message points to the top-up page. |
| 429, 503 | Up to 5 attempts with exponential backoff (1 s base, 60 s cap, 25% jitter); `Retry-After` is honoured up to 120 s. |
| 500, 502, 504, network errors | Up to 3 attempts with the same backoff. |
| 200 with an `error` object | Reported as a generation failure and retried by the agent loop. |

On top of that, the agent loop resends a step that failed for a transient reason up to 5 more times over about two minutes, showing each retry in the REPL and on the dashboard. Nothing from a failed attempt enters the history.

The HTTP client allows 15 s to connect. For streamed requests, the 600 s timeout bounds the wait for headers and for each read, not the whole stream, so long thinking steps are fine.

## Context budget

The API is stateless, so RaptorCode keeps the history locally and sends it with each request. After each tool round, it estimates the next prompt size:

- at **720K tokens** it warns once;
- at **786,432 tokens** (75% of the window) it compacts: it keeps the system prompt, the original task or mission, a marker saying how many messages were dropped, and the most recent messages. A tool result is never separated from the turn that requested it.

That leaves room for the 128K output budget plus a round of tool results inside the window.

## Context caching

DeepSeek caches prompt prefixes automatically. Because RaptorCode only appends to a conversation (compaction aside), later requests reuse the cached prefix. The REPL shows the context size and the cache-hit share, and `raptor run` prints input, cached and output token totals.

## Not used

- **Strict tool mode (beta)** needs the beta base URL and schemas with every property required. It may be added as an option.
- **Server-side web tools** run outside the local tool policy, so agents don't declare them.
- **Chat prefix completion and fill-in-the-middle (beta)** are not used.
