trading_agent.backends.deepseek_backend
¶
A backend for DeepSeek's API, which speaks OpenAI's Chat Completions protocol with three differences that matter here.
- Thinking mode, on by default, returns the model's thinking as reasoning_content. When tools are offered, DeepSeek requires that thinking to be sent back with every earlier assistant message, in every later request, or it refuses with a 400 error; the conversation keeps it for that reason.
- Prompt tokens are priced by whether DeepSeek's cache held them, and off-peak hours cost less. The usage report gives prompt_cache_hit_tokens and prompt_cache_miss_tokens, so each call's cost is worked out from the prices in trading_agent.toml.
- A 402 Insufficient Balance error means the account's credit is used up. The router then leaves DeepSeek alone for 15 minutes before trying again, so a top-up is noticed without a request on every turn.
Typical usage example:
backend = DeepSeekBackend('deepseek', backend_configuration, api_key) reply = await backend.complete(conversation, registry.definitions(), on_text)
DeepSeekBackend
¶
Bases: OpenAICompatibleBackend
One DeepSeek model, with its thinking sent back and its cost worked out per call.
Creates the backend without contacting DeepSeek.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The backend's name in trading_agent.toml. |
required |
backend_configuration
|
BackendConfiguration
|
Its settings, of kind "deepseek". |
required |
api_key
|
str
|
The DeepSeek API key. |
required |
clock
|
Callable[[], datetime] | None
|
The source of the current UTC time, replaceable in tests; None uses the real clock. |
None
|
Source code in src/trading_agent/backends/deepseek_backend.py
is_peak
¶
Says whether a moment falls in DeepSeek's peak hours: the configured UTC periods, Monday to Friday.
Chinese public holidays, which DeepSeek prices as off-peak, are not known here, so those days are counted at the peak price.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
moment
|
datetime
|
The moment, with a time zone. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True in a peak period on a weekday. |