How to Fix 'authentication failed when scaling' in CrewAI (TypeScript)
When CrewAI throws authentication failed when scaling, it usually means your agent or runtime is trying to call a remote service with missing, expired, or mismatched credentials. In TypeScript projects, this tends to show up when you move from local execution to a scaled worker, serverless function, or background job and the auth context is no longer being passed correctly.
The important detail: this is usually not a CrewAI “model bug”. It’s almost always an environment, token propagation, or client initialization problem.
The Most Common Cause
The #1 cause is initializing your CrewAI client or provider once in local code, then reusing it inside a scaled worker where the auth token is absent.
A common pattern is creating the client at module scope and assuming the token will still exist when the job runs elsewhere.
| Broken pattern | Fixed pattern |
|---|---|
| Auth read once at startup | Auth read per request/job |
| Token captured in stale closure | Token injected into worker context |
| Works locally, fails when scaled | Works in queue workers and serverless |
// ❌ Broken: token captured at import time
import { CrewClient } from "@crewai/typescript";
const client = new CrewClient({
apiKey: process.env.CREWAI_API_KEY,
});
export async function runTask() {
const result = await client.agents.create({
name: "SupportAgent",
goal: "Handle ticket triage",
});
return result;
}
// ✅ Fixed: resolve auth inside the execution boundary
import { CrewClient } from "@crewai/typescript";
export async function runTask(apiKey: string) {
if (!apiKey) {
throw new Error("Missing CREWAI_API_KEY in worker context");
}
const client = new CrewClient({ apiKey });
const result = await client.agents.create({
name: "SupportAgent",
goal: "Handle ticket triage",
});
return result;
}
If you’re using a queue or scaling layer, pass credentials explicitly into the job payload or load them from the worker’s environment. Don’t assume the parent process environment survives serialization.
Other Possible Causes
1. Wrong environment variable name
CrewAI integrations often fail with auth errors because the key exists locally under one name and your deployment expects another.
// ❌ Broken
const apiKey = process.env.CREW_AI_KEY;
// ✅ Fixed
const apiKey = process.env.CREWAI_API_KEY;
If your logs show something like:
- •
401 Unauthorized - •
authentication failed when scaling - •
CrewAIError: Invalid API key
check for typos first.
2. Token expires before the scaled job starts
This happens when you mint short-lived tokens in the web tier and enqueue work that runs later.
// ❌ Broken: short-lived token passed to delayed job
await queue.add("scale-agent", {
apiKey: sessionToken,
});
// ✅ Fixed: use a service credential for workers
await queue.add("scale-agent", {
apiKey: process.env.CREWAI_SERVICE_API_KEY,
});
For background jobs, use a service account or long-lived machine credential. User session tokens are fine for immediate requests, not for deferred execution.
3. Worker container does not have secrets mounted
In Kubernetes, ECS, Docker Compose, or serverless functions, the app may run fine locally but fail in production because the worker never received secrets.
# ❌ Broken deployment snippet
env:
- name: NODE_ENV
value: production
# ✅ Fixed deployment snippet
env:
- name: NODE_ENV
value: production
- name: CREWAI_API_KEY
valueFrom:
secretKeyRef:
name: crewai-secrets
key: apiKey
If you see CrewClient throwing auth failures only in workers, compare env vars between web and worker pods.
4. Mixing user-scoped and service-scoped clients
A subtle bug appears when one part of your app uses a user JWT and another part uses an API key. The request reaches CrewAI with a valid-looking token that belongs to the wrong auth domain.
// ❌ Broken
const client = new CrewClient({
apiKey: req.headers.authorization?.replace("Bearer ", ""),
});
// ✅ Fixed
const client = new CrewClient({
apiKey: process.env.CREWAI_API_KEY,
});
Use user tokens only if the SDK explicitly supports delegated auth for that endpoint. For scaling workers, keep it simple: machine-to-machine auth.
How to Debug It
- •
Log the exact auth source
- •Print where the key comes from before creating
CrewClient. - •Confirm whether it’s
process.env, request headers, or job payload.
- •Print where the key comes from before creating
- •
Reproduce inside the worker
- •Don’t test only in your API route.
- •Run the same task inside the queue consumer or serverless handler where scaling happens.
- •
Inspect the raw error
- •Look for
401,403,invalid_api_key, ormissing authentication. - •If you only see
authentication failed when scaling, wrap the SDK call and logerror.causeif available.
- •Look for
- •
Compare environments
- •Check these values in both web and worker processes:
- •
CREWAI_API_KEY - •region/tenant config
- •any proxy settings
- •
- •A mismatch here is enough to trigger auth failure during scale-out.
- •Check these values in both web and worker processes:
Example diagnostic wrapper:
try {
const client = new CrewClient({ apiKey });
return await client.agents.create({ name: "BillingAgent", goal: "Resolve invoices" });
} catch (error) {
console.error("CrewAI auth debug", {
message: error instanceof Error ? error.message : String(error),
apiKeyPresent: Boolean(apiKey),
nodeEnv: process.env.NODE_ENV,
});
throw error;
}
Prevention
- •Create clients inside the execution boundary of each worker/job, not at module load time.
- •Use service credentials for scaled workloads; don’t pass user session tokens into background jobs.
- •Add startup checks that fail fast if
CREWAI_API_KEYis missing in any runtime that can execute agents.
If you want this to stop happening permanently, treat auth as part of worker configuration, not application code. That’s where most TypeScript + CrewAI scaling failures start.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit