Edge Function using Anthropic / Claude API returning 401 status code

I’m encountering an error with my edge function that I’m struggling to fix. I am using the @anthropic-ai**/sdk** / Claude API and it works successfully on my localhost with a “netlify dev” run. However, once I deploy the function to netlify / production, it fails with this error:

Sep 18, 12:59:36 PM: 01K5DC6P error [xyz] Error: 401 status code (no body)
at _APIError.generate (file:///root/.netlify/edge-functions/.vendor/bundled-@anthropic-ai_sdk.js:113:14)
at Anthropic.makeStatusError (file:///root/.netlify/edge-functions/.vendor/bundled-@anthropic-ai_sdk.js:3599:21)
at Anthropic.makeRequest (file:///root/.netlify/edge-functions/.vendor/bundled-@anthropic-ai_sdk.js:3742:24)
at eventLoopTick (ext:core/01_core.js:178:7)

I have added console logging and confirm the API_KEY is correct set/accessed via the env. The edge function is at it’s basics:

import { Anthropic } from "@anthropic-ai/sdk";
const api_key = Netlify.env.get('CLAUDE_API_KEY');
const anthropic = new Anthropic({ apiKey: api_key });
const msg = await anthropic.messages.create({
	model: 'claude-3-7-sonnet-latest',
	max_tokens: 250,
	messages: [ { role: 'user', content: 'some fancy prompt' } ]
});

I am using Hono if that is of any significance and have the basics set like this:

app.use('/*', cors())
app.get('/*', async (c) => { callClaude() })
export default handle(app)

Where the callClaude() code is the prior codeblock.

I’ve tried generating new API keys and it does not change anything. There’s nothing really online about this error - either on Anthropic or Netlify docs so I’m not sure where the error is at.

Because the code works in the dev environment, I’m wondering if there is something being done with production edge-functions that is breaking the anthropic authentication. There is no further information in the log to suggest what is going wrong.

Any thoughts or suggestions?

Well, I’ve answered my own question. I found out the Anthropic package has a debug logging mode. Amongst the request log I noticed:

url: "``https://xyz.netlify.app/.netlify/ai/v1/messages``",

which looked a bit strange as I thought it should be the anthropic URL. It is set via the variable ANTHROPIC_BASE_URL which is not set in my project settings. I manually override this URL (with the default value in their library):

const anthropic = new Anthropic({ apiKey: api_key, logLevel: 'debug', baseURL: '``https://api.anthropic.com``' });

And the requests now work.

So, it would appear, somewhere on Netlify the ANTHROPIC_BASE_URL variable is being set, but perhaps there is something else to it? May be a bug