Skip to content

deepseek

robotsix_llmio.deepseek

Direct-API DeepSeek transport layer.

Talks to https://api.deepseek.com directly with a DeepSeek-issued key, reusing the DeepSeek-side behaviours (per-level thinking policy, reasoning_content round-trip) while dropping OpenRouter-only machinery (provider-routing kwargs, usage.include, server usage.cost). Cost is computed client-side from configurable sticker prices.

The model/provider (which import pydantic-ai and thus opentelemetry) are loaded lazily via PEP 562 __getattr__ so importing this package stays cheap.

DeepseekAPIError

Bases: RobotsixLLMIOError

Error from the direct DeepSeek client (HTTP, auth, malformed response).

DeepseekModel

Bases: OpenAIChatModel

OpenAIChatModel for the direct DeepSeek API.

Carries the thinking-mode reasoning_content round-trip and records cost client-side (DeepSeek returns no usage.cost). The provider stamps the per-level model id and :attr:pricing after construction.

DeepseekPricing

Bases: BaseModel

Model-id → :class:ModelPrices map used to compute cost client-side.

Populated from provider config so the sticker prices are updatable without a code change; unknown/unpriced model ids resolve to None and record no cost (see :func:record_deepseek_cost).

from_mapping classmethod

from_mapping(mapping)

Build a pricing config by merging mapping over the baked defaults.

mapping is a model-id → {"input", "output", "cache_read"} mapping (e.g. a tier's provider_kwargs["pricing"]). Entries override the baked defaults for the same model id; unspecified models keep theirs.

for_model

for_model(model_id)

Return the prices for model_id, or None when unpriced.

ModelPrices

Bases: BaseModel

Per-1M-token USD prices for a single DeepSeek model id.

Attributes:

Name Type Description
input float

Price per 1M uncached (cache-miss) input tokens.

output float

Price per 1M output/completion tokens.

cache_read float

Price per 1M cache-hit input tokens (DeepSeek bills a cache read at a fraction of the full input rate).

DeepseekProvider

Bases: LLMProvider

Builds cost-instrumented direct-API DeepSeek models from a model name.

new_model

new_model(*, model=None, level=0)

Build a model, returning (model, http_client).

Parameters:

Name Type Description Default
model str | None

The concrete DeepSeek model id (e.g. "deepseek-chat" or "deepseek-reasoner").

None
level int

Capability level (1, 2, 3) forwarded to :meth:_post_build_model for the per-level thinking policy. 0 means unknown / direct new_model() call.

0

The returned http_client is the timeout-configured client backing the model; the caller owns closing it.

compute_cost

compute_cost(usage, prices)

Compute USD cost from a DeepSeek usage object and prices.

Splits the input tokens into cache-hit (billed at cache_read) and cache-miss (billed at input) using DeepSeek's prompt_cache_hit_tokens / prompt_cache_miss_tokens fields, falling back to prompt_tokens_details.cached_tokens and finally to treating every input token as uncached. Returns None when there is no usage or no prices.

record_deepseek_cost

record_deepseek_cost(response, pricing)

Compute client-side cost for response and record it via :func:~robotsix_llmio.core.cost.record_cost with provider="deepseek".

Looks up per-1M prices for the response's model id; an unknown/unpriced model id records nothing (debug log) and never raises.