BerriAI/litellm · error · AuthenticationError

CohereException

Error message

CohereException - {original_exception.message}

What it means

_map_cohere_exception maps error strings 'invalid api token' or 'No API key provided.' to litellm.AuthenticationError ('CohereException - <original message>'). This is Cohere's platform rejecting (or not receiving) your API key on api.cohere.com. litellm sends the key from COHERE_API_KEY or the api_key parameter, so the failure is almost always a missing/typo'd environment variable or an invalidated trial key.

Solutions

  1. Set the key: export COHERE_API_KEY=<key> from dashboard.cohere.com > API keys, or pass api_key explicitly.
  2. Verify it works directly: curl -H "Authorization: Bearer $COHERE_API_KEY" 'https://api.cohere.com/v1/models'.
  3. If the trial expired, create a new key on a paid/production account and update the secret store.
  4. For EU endpoints set the correct api_base alongside the key.

Example fix

# before
resp = litellm.completion(model="cohere/command-r-plus", messages=msgs)  # AuthenticationError: No API key provided.

# after
import litellm, os
resp = litellm.completion(
    model="cohere/command-r-plus",
    messages=msgs,
    api_key=os.environ["COHERE_API_KEY"],
)
Defensive patterns

Strategy: validation

Validate before calling

import os

def require_cohere_key() -> str:
    key = os.environ.get("COHERE_API_KEY")
    if not key:
        raise RuntimeError("COHERE_API_KEY is not set — cohere calls will fail with AuthenticationError")
    return key

Type guard

import litellm

def is_authentication_error(e: BaseException) -> bool:
    return isinstance(e, litellm.AuthenticationError)

Try / catch

try:
    resp = litellm.completion(model="cohere/command-r-plus", messages=msgs)
except litellm.AuthenticationError as e:
    logger.error("cohere rejected API key: %s", e)
    notify_ops("COHERE_API_KEY invalid or expired")
    raise

Prevention

When it happens

Trigger: model='cohere/command-r-plus' (or command-a, command-r) without COHERE_API_KEY set; a trial key that expired when the trial ended; a revoked or regenerated key still cached in env; passing api_key=None because os.getenv was misspelled.

Common situations: Local works but deployed container lacks COHERE_API_KEY; free-trial keys expiring after evaluation; CI pipelines masking the variable incorrectly (empty string ≠ unset, both fail here); using a key from a different Cohere environment (EU vs US base URLs).

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/e3f3159332a52b35. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:1334

            message=f"Cloudflare Exception - {original_exception.message}",
            llm_provider="cloudflare",
            model=model,
            response=getattr(original_exception, "response", None),
        )


def _map_cohere_exception(
    *,
    model: str,
    original_exception: _ProviderHTTPException,
    custom_llm_provider: str,
    error_str: str,
    exception_type: str,
    exception_provider: str,
    extra_information: str,
) -> None:
    if "invalid api token" in error_str or "No API key provided." in error_str:
        raise AuthenticationError(
            message=f"CohereException - {original_exception.message}",
            llm_provider="cohere",
            model=model,
            response=getattr(original_exception, "response", None),
        )
    elif "invalid type: parameter" in error_str:
        raise BadRequestError(
            message=f"CohereException - {original_exception.message}",
            llm_provider="cohere",
            model=model,
            response=getattr(original_exception, "response", None),
        )
    elif "too many tokens" in error_str:
        raise ContextWindowExceededError(
            message=f"CohereException - {original_exception.message}",
            model=model,
            llm_provider="cohere",
            response=getattr(original_exception, "response", None),

View on GitHub (pinned to 77b7c6c40c)