DeepSeek Harness: Build an LLM Adapter
A DeepSeek Harness LLM adapter translates a provider's stream into the Harness protocol, registers a provider route, and must preserve tool arguments, usage, cancellation and replay semantics across turns.
DeepSeek Harness: where does an adapter fit?
The adapter cookbook presents LlmAdapter as the provider boundary. The capability seam catalog describes ctx.llm among the core services. The adapter supplies a route to that service; the agent and consumers use the shared stream vocabulary instead of each implementing the provider protocol.
The cookbook points to packages/llm/llm-deepseek for direct HTTP with SSE framing and packages/llm/llm-pi-ai for a library wrapper. Read StreamChunk in packages/llm/llm/src/types.ts before implementation. Choose a reference based on the provider transport, but implement the contract even when a library has already parsed the response.
DeepSeek Harness: what does the plugin register?
The cookbook's shape subclasses LlmAdapter and implements async * stream(options: GenerateOptions): AsyncIterable<StreamChunk>. The plugin declares inject = ['llm']; its apply registers an instance using ctx.llm.registerAdapter(['my-provider'], ...). This is a structural outline, not a complete implementation or a real provider configuration.
options.provider selects the route and options.model identifies the provider's model. A dynamic catalog adapter can serve new models without lifecycle reconfiguration. Registration is an effect, so unloading the plugin can undo it. One adapter owns a provider route; duplicate routes throw, and registering multiple routes is all-or-nothing. Test conflict and disposal behavior in the composition where the plugin will run.
| Boundary | Adapter responsibility | Failure to avoid |
|---|---|---|
| Provider request | Map supported options and pass cancellation | Silently dropping an unsupported option |
| Stream blocks | Preserve first-seen index and raw tool JSON | Changing a tool call's identity or parsing away fragments |
| Completion | Emit usage before finish, then stop | A trailing chunk after finish |
| Follow-up | Validate native replay state when needed | Assuming state from model names alone |
DeepSeek Harness: how should I implement the stream?
- Study the provider's documented request, streaming and error formats alongside the installed Harness
StreamChunkcontract. Decide whether direct transport or a maintained library fits the provider. Identify required credentials and capabilities before writing the mapper. - Map
GenerateOptionsto the provider request. Passoptions.signalto fetch or the SDK. If the provider cannot honor an option, throwLlmErrorwithUNSUPPORTED_OPTION, rather than silently ignoring the request. - Allocate block indexes in first-seen stream order and reuse the same index for every delta of that block. Tool-call arguments remain raw JSON strings; emit fragments as
argumentsDelta. If the provider library returns parsed objects, re-stringify atblock-endas the cookbook specifies. - Buffer completion and usage until the provider's end-of-stream marker when needed. Emit
usagebeforefinishand nothing afterfinish; this handles providers that send usage-only chunks late. Verify this ordering with a fixture that includes trailing usage. - Distinguish transport or protocol failure from an in-band provider failure. The documented paths are to throw from
stream()using anLlmErrorwith a stable code, or finish withkind: 'error'or'aborted'. Choose and document the mapping for each failure class. Exercise cancellation, malformed responses and provider errors. - Register the route in a plugin and test a complete request, including a tool call and a follow-up. Check duplicate registration, unloading and supported model options as well as ordinary text streaming.
Keep fixtures small enough to identify which event violates the contract. For a tool-call fixture, assert the sequence of fragments and the final block identity as well as the resulting arguments; a correct final text alone can conceal a broken intermediate stream. These steps are a contract checklist, not a full provider implementation. The transport must also follow the specific provider's documentation. The two reference adapters are useful for comparing handling of framing and libraries, but their provider-specific request fields should not be copied without verification.
DeepSeek Harness: how do replay and model capabilities work?
Some providers require native response identifiers, signatures or other metadata on a later call. The cookbook says to put the minimal lossless JSON projection in finish.replayState and validate it when rebuilding history. LlmRuntime passes that state only when the historical route and the target route are currently owned by the exact same adapter instance. The adapter still decides whether restoring it across models or providers is legal. If state is absent, names alone are not evidence that native replay is possible.
For model metadata, the cookbook describes resolveModel() with provider and model identity and optional context and reasoning fields. Declare a configured defaultEffort only when one exists, and honor its optional abort signal. Reasoning efforts are ordered opaque identifiers mapped by the adapter to provider requests. Provider-specific thinking toggles remain in adapter configuration; do not assume all providers have the same capabilities or effort labels.
Test replay with both present and absent state, and verify that an incompatible follow-up does not quietly reuse a provider-specific signature. Test capability lookup separately from generation. A valid stream today does not by itself demonstrate that the next turn can be reconstructed correctly.
Which tests expose protocol mistakes?
Use controlled fixtures for interleaved blocks, fragmented tool arguments, a trailing usage chunk, an in-band failure and a transport failure. Assert the order and identity of emitted chunks, not only the final text. Test an unsupported option explicitly and check that cancellation reaches the chosen transport. For replay, cover both present and absent native state and an incompatible target. Keep provider credentials and real signatures out of fixtures. These cases exercise the seam; a separate integration test is needed to establish that the selected provider actually accepts the mapped request.
DeepSeek Harness: what are the limits and safety boundaries?
Use the documented Cordis configuration mechanism for secrets, with environment fallbacks where appropriate, rather than adding ad hoc key files. Keep credentials and real request bodies out of test fixtures and reports. A registered route is not proof that provider data handling or access controls meet deployment requirements.
The cookbook describes two verified implementations and the Harness contract, not every provider's API. Check installed-version types, provider documentation and failure behavior before release. Never assume the provider accepts an unsupported option, returns a complete usage record, or allows replay across models. Test with controlled inputs and review network destinations and data sent to the provider.
DeepSeek Harness: which terms matter?
- Adapter: the component translating between a provider and the Harness LLM contract.
- Route: a provider identifier registered to an adapter instance.
- Stream chunk: one structured part of a model response.
- Replay state: native metadata needed to reconstruct a compatible follow-up.
- Capability resolution: provider/model metadata used to describe supported context and reasoning.
Frequently asked questions
When must usage be emitted?
Emit usage before finish and no chunks after finish.
Can unsupported options be ignored?
No. The cookbook specifies an LlmError with UNSUPPORTED_OPTION.
Is replay state always passed between routes?
No. Runtime passes it only when both routes are owned by the exact same adapter instance.
Sources
Every claim in this article was checked against the sources below. Community discussions are user reports, not official behavior.
This is an independent community article, not official DeepSeek documentation. Listing a plugin in the registry is not a security review.