Skip to content

trading_agent.backends.deepseek_backend

A backend for DeepSeek's API, which speaks OpenAI's Chat Completions protocol with three differences that matter here.

  1. 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.
  2. 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.
  3. 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

DeepSeekBackend(name, backend_configuration, api_key, clock=None)

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
def __init__(
    self,
    name: str,
    backend_configuration: agent_configuration.BackendConfiguration,
    api_key: str,
    clock: Callable[[], datetime.datetime] | None = None,
):
    """Creates the backend without contacting DeepSeek.

    Args:
        name (str): The backend's name in trading_agent.toml.
        backend_configuration (agent_configuration.BackendConfiguration): Its settings, of kind "deepseek".
        api_key (str): The DeepSeek API key.
        clock (Callable[[], datetime.datetime] | None): The source of the current UTC time, replaceable in tests; None uses the real clock.
    """
    super().__init__(name, backend_configuration, api_key)
    self._configuration = backend_configuration
    self._clock = clock or self._utc_now

is_peak

is_peak(moment)

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.

Source code in src/trading_agent/backends/deepseek_backend.py
def is_peak(self, moment: datetime.datetime) -> bool:
    """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.

    Args:
        moment (datetime.datetime): The moment, with a time zone.

    Returns:
        bool: True in a peak period on a weekday.
    """
    utc = moment.astimezone(datetime.UTC)
    if utc.weekday() > _LAST_WEEKDAY:
        return False
    minute = utc.hour * 60 + utc.minute
    for period in self._configuration.peak_hours_utc:
        start_text, _, end_text = period.partition('-')
        start = self._minutes(start_text)
        end = self._minutes(end_text)
        if start <= minute < end:
            return True
    return False