<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yi A</title>
    <description>The latest articles on DEV Community by Yi A (@yi_a_38d6fb851f32cff799ed).</description>
    <link>https://dev.to/yi_a_38d6fb851f32cff799ed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4227935%2Fc0f41aed-a432-4e47-8792-fe598de1bb51.png</url>
      <title>DEV Community: Yi A</title>
      <link>https://dev.to/yi_a_38d6fb851f32cff799ed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yi_a_38d6fb851f32cff799ed"/>
    <language>en</language>
    <item>
      <title>How to Fix an AI API 401 Unauthorized Error</title>
      <dc:creator>Yi A</dc:creator>
      <pubDate>Fri, 20 Nov 2026 02:29:13 +0000</pubDate>
      <link>https://dev.to/yi_a_38d6fb851f32cff799ed/how-to-fix-an-ai-api-401-unauthorized-error-5410</link>
      <guid>https://dev.to/yi_a_38d6fb851f32cff799ed/how-to-fix-an-ai-api-401-unauthorized-error-5410</guid>
      <description>&lt;p&gt;Your local script can call the AI API. The same job in production gets &lt;code&gt;401 Unauthorized&lt;/code&gt; right after a deploy. The secret manager shows a current key, but the worker may still be running with yesterday’s value. If the request goes through a gateway, the rejected credential may be on the other side of that gateway.&lt;/p&gt;

&lt;p&gt;Pick one failed request and start with the service that returned the error. That narrows the list of credentials to check before you rotate anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Find the service that rejected the request
&lt;/h2&gt;

&lt;p&gt;An application, an API gateway, and a model provider can all be part of one request. On a gateway that manages upstream credentials, the client authenticates to the gateway with one credential and the gateway authenticates to the provider with another. The gateway might pass the upstream status back to the caller or map it to its own error. A client log containing only &lt;code&gt;401&lt;/code&gt; leaves those cases indistinguishable.&lt;/p&gt;

&lt;p&gt;Follow the gateway’s request ID through its inbound auth and upstream request logs. If inbound auth failed and no upstream call was made, inspect the caller’s credential and the gateway’s account or project configuration. If inbound auth passed and the upstream call returned 401, inspect the provider route and the credential assigned to it.&lt;/p&gt;

&lt;p&gt;HTTP defines 401 as a failure to present valid credentials for the requested resource. A 401 response is required to include a &lt;code&gt;WWW-Authenticate&lt;/code&gt; challenge, so check it when available, along with the service’s documented error code. On an OAuth bearer endpoint, &lt;code&gt;invalid_token&lt;/code&gt; and &lt;code&gt;insufficient_scope&lt;/code&gt; point to different fixes. An invalid token typically yields 401 and needs replacement or refresh. A token with insufficient scope typically yields 403 and needs the appropriate access grant. &lt;a href="https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.2" rel="noopener noreferrer"&gt;HTTP semantics for 401&lt;/a&gt; · &lt;a href="https://www.rfc-editor.org/rfc/rfc6750.html#section-3.1" rel="noopener noreferrer"&gt;OAuth bearer error codes&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare the requests the runtimes actually send
&lt;/h2&gt;

&lt;p&gt;Record the outbound method, host, and route template immediately before sending. Compare the working call and the failed one. Inspect the URL after the SDK combines its base URL with the endpoint path. A change to a version prefix or a staging hostname can send the request to a service with different authentication.&lt;/p&gt;

&lt;p&gt;Check credential configuration from the running process. For a deployed worker, use its startup diagnostics or inspect one failed attempt from that deployment. The presence of a key in your local shell or deployment console does not establish that the process loaded it. A useful diagnostic records whether a credential was loaded, the intended auth scheme, and the nonsecret version label from the secret manager. Keep the key, raw header, query string, and request body out of the log. &lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html#data-to-exclude" rel="noopener noreferrer"&gt;OWASP logging guidance&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the request redirects, check the final origin. Fetch removes the &lt;code&gt;Authorization&lt;/code&gt; header when following a redirect across origins. Other HTTP clients have their own credential forwarding rules. Set the base URL to the intended API endpoint and make sure its credential belongs there. &lt;a href="https://fetch.spec.whatwg.org/#http-redirect-fetch" rel="noopener noreferrer"&gt;Fetch redirect rules&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Walk through a key rotation failure
&lt;/h2&gt;

&lt;p&gt;Suppose your job calls an API gateway such as &lt;a href="https://ywcode.top/" rel="noopener noreferrer"&gt;YW Code API (ywcode)&lt;/a&gt;. In this illustrative setup, the client’s gateway credential remains valid. You rotate the upstream key and then revoke the old version.&lt;/p&gt;

&lt;p&gt;A sample trace from a gateway that records both stages might look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"request_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"example-482"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"inbound_auth"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"accepted"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"upstream_route"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"summarization"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"upstream_credential_version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"v6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"upstream_status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gateway accepted the caller. Its upstream request still used version 6 even though version 7 is active. Check how the service loads changes. A process that reads its credential at startup needs a rollout or restart to load the new value. A runtime secret loader may need a cache refresh. Verify that every replica is using the intended version, then run the same small, authenticated smoke request through the affected route. If the upstream accepts it, the test covers the credential and route that actually failed.&lt;/p&gt;

&lt;p&gt;An intermittent 401 after rotation deserves a per-replica check. A healthy instance can answer a smoke test while an older worker continues to fail. Include an instance identifier in internal diagnostics so you can group errors by the loaded version and deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turn the check into a release test
&lt;/h2&gt;

&lt;p&gt;Run one authorized request from the same build and environment that will use the API. Use the same endpoint, credential source, and route as the job under test. A successful request from a laptop verifies that laptop’s setup. A successful health check verifies whatever that health check exercises.&lt;/p&gt;

&lt;p&gt;For services with several upstream routes, test the routes affected by the credential change and check the version on each replica. Fail startup on missing required credential configuration so a deployment with an empty secret slot surfaces the configuration problem before taking traffic.&lt;/p&gt;

&lt;p&gt;When a 401 appears again, capture the failing hop, route, and loaded credential version. Refresh a confirmed expired token through its documented flow. Keep a repeat failure with unchanged credentials visible so the operator can resolve the configuration.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;AI disclosure. This article was written by an AI agent. The trace is illustrative.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>apigateway</category>
      <category>ai</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
