Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

DeepSeek Harness LLM Adapter: Connecting Any Model

DeepSeek Harness LLM Adapter: Connecting Any Model

There is more than one model provider besides DeepSeek; to connect dsh to your own model endpoint, you need to write an LLM adapter.

In this section we will cover the LlmAdapter abstract class, the stream() method, route registration, and cordis.yml configuration.


What is an LLM adapter

An LLM adapter is a class that inherits from LlmAdapter and implements the stream() method.

It converts Harness's provider-agnostic requests into API calls for a specific provider.

It then converts the response back into Harness chunks, that is, StreamChunk.

Therefore the agent-loop consumes a unified interface and does not care which API is behind it.

LLM adapter seam diagram

At the top is the agent-loop, which consumes the provider-agnostic streaming generation service.

In the middle is the ctx.llm registry, which maintains the abstract contract of LlmAdapter.

At the bottom are the individual adapters, each interfacing with a different API format.

When registering, use ctx.llm.registerAdapter(['my-provider'], adapter) to bind the route.


Minimal implementation

The official documentation gives a minimal skeleton, which we will follow and add comments to.

stream() is the core and returns an asynchronous generator AsyncIterable.

Three steps of comments mark the standard flow: convert the message format, call the streaming API, and convert the response into StreamChunk.

Example

// File path: src/my-llm-adapter.ts

import

type

{

Context

}

from

'@deepseek-ai/cordis'

import

Schema from

'@deepseek-ai/schemastery'

import

{

LlmAdapter

,

type GenerateOptions

,

type StreamChunk

}

from

'@deepseek-ai/dsh-llm'

// Adapter: inherit the abstract class, implement stream()

class

MyAdapter

extends

LlmAdapter

{

  private apiKey

:

string

  constructor

(

apiKey

:

string

)

{

   

super

(

)

   

this

.

apiKey

=

apiKey

 

}

 

// stream() returns an async generator, yielding StreamChunk piece by piece

  async

stream

(

options

:

GenerateOptions

)

:

AsyncIterable

<

StreamChunk

{

// 1. Convert options.messages to the provider format.

   

// 2. Call the streaming API.

   

// 3. Convert the response into StreamChunk values.

 

}

}

// Plugin config: both apiKey and providers are required

export

interface

Config

{

  apiKey

:

string

  providers

:

string

[

]

}

// A Schemastery schema of the same name validates the config at load time

export

const

Config

:

Schema

<

Config

=

Schema.

object

(

{

  apiKey

:

Schema.

string

(

)

.

required

(

)

,

  providers

:

Schema.

array

(

Schema.

string

(

)

)

.

required

(

)

,

}

)

export

const

name

=

'my-llm-adapter'

// Declare a dependency on the llm service to ensure ctx.llm is ready

export

const

inject

=

[

'llm'

]

export

function

apply

(

ctx

:

Context

,

config

:

Config

)

{

 

const

adapter

=

new

MyAdapter

(

config.

apiKey

)

 

// Bind the provider route list to this adapter

  ctx.

llm

.

registerAdapter

(

config.

providers

,

adapter

)

}

apiKey comes from the plugin config, avoiding hardcoding the key in code.

The providers array declares which provider routes this adapter handles.

In apply, first new MyAdapter(config.apiKey), then registerAdapter to complete registration.

inject: ['llm'] ensures apply runs only after the ctx.llm service is ready.

stream() returns an async generator, using yield to emit data piece by piece.

The next article explains the StreamChunk chunking protocol in detail.


GenerateOptions: what the adapter receives

stream() receives the GenerateOptions exported by the repository.

It contains the model, reasoning effort, conversation history, system prompt, tool schema, generation parameters, stop sequences, and abort signal.

For complete fields, refer to the TypeScript types exported by @deepseek-ai/dsh-llm.

Field Description
provider Selecting a registered adapter
model Model ids owned by the adapter, no registration needed at startup
messages Conversation history
system prompt System prompt
tools Tool schema
reasoning Reasoning effort IDs owned by the adapter (optional)
signal Abort signal, used to fully stop cancellation and resource release

The adapter must map supported fields to the concrete API.

If a field cannot be supported, throw an LlmError with a stable code; never silently drop it.

Unsupported fields should raise an explicit error rather than being quietly ignored; otherwise the model gets incomplete results.


Registering the adapter

In apply, call ctx.llm.registerAdapter to complete route registration.

Example

// File path: src/my-llm-adapter.ts (registration snippet)

ctx.

llm

.

registerAdapter

(

[

'my-provider'

]

,

adapter

)

The first argument is the list of provider routes the adapter handles.

GenerateOptions.provider selects the registered adapter.

GenerateOptions.model passes the model id owned by the adapter, which needs no registration at lifecycle startup.

If the adapter can publish model options to the selector, it can override listModels().

To return exact provider and model identity, you can override resolveModel(provider, model, signal?).

resolveModel returns the exact provider, model identity, and optional context and reasoning metadata in one query.

reasoning metadata describes the model's optional reasoning efforts: ordered opaque IDs, display names, and optional config defaults.

Preserve the authoritative option list given by the adapter, including off returned by the upstream capability API; do not promote these values to core enums.

Async queries must honor the optional signal so cancellation and resource release settle completely.

The service validates the aggregated result: an explicitly specified but unsupported reasoning effort is rejected before stream() is called.

Omitting reasoning means the model has no optional reasoning effort capability.


Using it in cordis.yml

Load the adapter plugin together with agent-loop, and have agent-loop use the new provider and model.

Example

File path: cordis.yml

Load the adapter plugin, with apiKey read from an environment variable

:

my-llm

  name

:

'./src/my-llm-adapter.ts'

  config

:

    apiKey

:

!!js

process.env.MY_API_KEY

    providers

:

     - my-provider

Configure agent-loop to use the new adapter's provider and model

:

agent-loop

  name

:

'@deepseek-ai/dsh-agent-loop'

  config

:

    agents

:

      - id

:

main

        provider

:

my-provider

        model

:

my-model-v1

The my-llm plugin's config.apiKey is read from the environment variable MY_API_KEY and is not written to disk.

The providers array registers the my-provider route to MyAdapter.

agent-loop's agents.main configures provider and model, so generation requests hit the new adapter.


Practical references

The repository has two ready-made complete implementations to compare.

llm-deepseek adapts the DeepSeek API using the OpenAI-compatible format.

llm-pi-ai adapts Pi AI, a different API format.

Comparing these two adapters shows how the same harness contract is implemented on top of different provider SDKs.

Reading llm-deepseek first and llm-pi-ai second makes it easiest to see the seam idea of "unchanged contract, varied implementations".


Summary and self-test

In one sentence: LLM adapter = inherit LlmAdapter + implement stream() + registerAdapter route registration + cordis.yml configuration.

Self-test 1: What is the return type of stream()?

Self-test 2: What does the first argument of registerAdapter represent?

Self-test 3: Which config items does agent-loop use to select the new adapter?