BerriAI/litellm · error · ValueError
Missing Cloudflare API Key - A call is being made to…
Error message
Missing Cloudflare API Key - A call is being made to cloudflare but no key is set either in the environment variables or via params
What it means
ValueError raised in the Cloudflare adapter's validate_environment when the api_key argument is None — meaning neither the api_key parameter nor any environment fallback produced a credential. Unlike many providers, this adapter refuses to build headers without a key rather than sending an anonymous request that would fail upstream.
Solutions
- Set the key in the environment: export CLOUDFLARE_API_KEY=<token> (a Workers AI / API token with Workers AI permission)
- Or pass it per call: litellm.completion(model="cloudflare/...", api_key=token, messages=messages)
- Verify with printenv that the key exists in the process that runs litellm
- If using the Router/proxy, set the key in the deployment environment or model_list entry, not just locally
Example fix
# before litellm.completion(model="cloudflare/@cf/meta/llama-3.1-8b-instruct", messages=messages) # ValueError: Missing Cloudflare API Key # after os.environ["CLOUDFLARE_API_KEY"] = "your-workers-ai-token" litellm.completion(model="cloudflare/@cf/meta/llama-3.1-8b-instruct", messages=messages)
Defensive patterns
Strategy: validation
Validate before calling
import os
api_key = os.environ.get("CLOUDFLARE_API_KEY") or os.environ.get("CLOUDFLARE_AI_TOKEN")
if not api_key:
raise RuntimeError("Cloudflare API key missing; set CLOUDFLARE_API_KEY or pass api_key=")
litellm.completion(
model="cloudflare/@cf/meta/llama-3.1-8b-instruct",
messages=messages,
api_key=api_key,
) Try / catch
try:
litellm.completion(model="cloudflare/@cf/...", messages=messages)
except ValueError as e:
if "Missing Cloudflare API Key" in str(e):
litellm.completion(
model="cloudflare/@cf/...", messages=messages, api_key=get_key_from_secret_store()
)
else:
raise Prevention
- Set CLOUDFLARE_API_KEY (Workers AI token) wherever CLOUDFLARE_ACCOUNT_ID is set
- Use a startup credential check across all providers to catch missing keys before traffic
- Prefer explicit api_key= in short scripts so the dependency is visible
When it happens
Trigger: Calling a cloudflare/* model with no api_key passed and no CLOUDFLARE_API_KEY (or provider-prefixed equivalent) present in the environment at request time.
Common situations: Only CLOUDFLARE_ACCOUNT_ID was configured; key stored under a differently-named variable; env vars set in a local shell but not in Docker/CI; typos in the variable name.
Understand the failure class
Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.
Related errors
- Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to…
- DynamoAI API key is required. Set DYNAMOAI_API_KEY…
- EnkryptAI API key is required. Set ENKRYPTAI_API_KEY…
- Invalid proxy server token passed. valid_token=None.
- Malformed API Key passed in. Ensure Key has `Bearer `…
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/3c72b79a210fe6d6.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/cloudflare/chat/transformation.py:76
if trimmed.endswith("/ai/run"):
verbose_logger.warning(
"Cloudflare api_base ending in '/ai/run' is the legacy Workers AI path and no longer serves OpenAI-compatible requests; rewriting to the '/ai/v1' endpoint"
)
return f"{trimmed[: -len('/ai/run')]}/ai/v1"
return api_base
def validate_environment(
self,
headers: dict,
model: str,
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
api_key: str | None = None,
api_base: str | None = None,
) -> dict:
if api_key is None:
raise ValueError(
"Missing Cloudflare API Key - A call is being made to cloudflare but no key is set either in the environment variables or via params"
)
return super().validate_environment(
headers=headers,
model=model,
messages=messages,
optional_params=optional_params,
litellm_params=litellm_params,
api_key=api_key,
api_base=api_base,
)
def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:
return CloudflareError(
status_code=status_code,
message=error_message,
)
View on GitHub (pinned to 77b7c6c40c)