Two SDKs behind one interface
Every later day calls a model through the code you write here, so start with one interface that both providers fit behind. Each SDK has its own request and response shapes, and the interface hides those differences from the rest of WorkBot.
Study first
- In Anthropic's Messages API, learn
system,messagesandmax_tokens, howclient.messages.streamstreams a reply, and where the final message reports itsusage. - In OpenAI's Responses API, learn the matching fields,
instructions,inputandmax_output_tokens, along with its streaming events and theusageon the completed response. - Learn the sampling controls. A model that reasons takes a reasoning effort,
output_config.effortfor Claude (effort) andreasoning.effortfor OpenAI (reasoning models). A model that still acceptstemperaturetakes a value from 0 to 1, but the current Claude models reject it.
Build
- Create an API key in the Anthropic console and put it in
.env.localasANTHROPIC_API_KEY. AddOPENAI_API_KEYas well if you have an OpenAI key. - Write
src/lib/providers.tsaround aProvidertype, which has aname, a list ofModels and astream(completion, onText)method that resolves with the tokens used. EachModelcarries its id and its published price per million tokens, and the first model in the list is the default. - In the same file, write one factory per SDK,
anthropic()andopenai(), each of which returns aProvider. PassmaxRetries: 3to each SDK's constructor, so the SDK retries rate limits and server errors for you. - Test both factories without the network. Give each SDK a fake
fetchthat returns an event stream you write from the SDK's own event types. Then check that text deltas arrive in order, that usage comes from the final event, and that the request body carries the right fields.
Done when
pnpm testpasses for both providers.- A call to Haiku, Anthropic's small model, through
anthropic().streamprints a reply.
Technical notes
- Prices live on the model list and nowhere else. A model without a published price has none, so the cost report names it instead of guessing.
- Both SDKs retry 429 and 5xx responses with backoff, including Anthropic's 529 when the API is overloaded. After the third retry, the error reaches your code.
- The OpenAI stream ends with one of 3 events.
response.completedis a finished reply, andresponse.incompletemeans the reply was cut off but still reports usage.response.failedcarries the API's message, which the provider throws as an error. - Create each SDK object on first use rather than when the provider is chosen. Switching to a provider without a key then fails at the request, where the error can be shown, and not at the switch.


