BerriAI/litellm · error · RuntimeError

Failed to fetch AgentOps token

Error message

Failed to fetch AgentOps token: {response.text}

What it means

Raised by litellm's AgentOps OpenTelemetry preset when it exchanges your AGENTOPS_API_KEY for a short-lived JWT at the AgentOps auth endpoint and gets any non-200 status; the endpoint's response body is embedded in the RuntimeError, so the actual reason (invalid key, revoked key, upstream 5xx) is whatever the auth service replied. It fires inside _fetch_agentops_jwt during exporter construction, i.e. at tracing setup time, before any LLM call or span export happens. Because the call uses a dedicated one-shot httpx.Client with a 10s timeout, it cannot come from a shared connection pool being closed.

Solutions

  1. Verify AGENTOPS_API_KEY is current and active in the AgentOps dashboard, with no trailing whitespace or newline (print(len(key)) to check)
  2. Reproduce manually: POST {'api_key': key} to the same auth endpoint (_AGENTOPS_AUTH_ENDPOINT in litellm/integrations/otel/presets/agentops.py) with curl/httpx and read the raw status code and body
  3. Check container egress rules / HTTP(S)_PROXY settings so the POST can reach the AgentOps auth host
  4. If AgentOps is temporarily down, disable the agentops OTEL preset and re-enable later
  5. Upgrade litellm in case the auth endpoint or payload format changed between versions

Example fix

# before
os.environ['AGENTOPS_API_KEY'] = maybe_stale_key  # possibly revoked
setup_otel_with_agentops()  # RuntimeError: Failed to fetch AgentOps token: ...

# after
import httpx, os
key = os.environ.get('AGENTOPS_API_KEY', '').strip()
resp = httpx.post(AUTH_ENDPOINT, json={'api_key': key}, timeout=10)
if resp.status_code != 200:
    logger.warning('AgentOps auth preflight failed (%s): %s', resp.status_code, resp.text)
    # skip enabling the agentops exporter; run without it
else:
    setup_otel_with_agentops()
Defensive patterns

Strategy: try-catch

Validate before calling

import httpx, os

key = os.environ.get('AGENTOPS_API_KEY', '').strip()
if not key:
    raise RuntimeError('AGENTOPS_API_KEY missing; skip agentops exporter')
resp = httpx.post(AUTH_ENDPOINT, json={'api_key': key}, timeout=10)
if resp.status_code != 200:
    logger.warning('AgentOps auth preflight failed: %s %s', resp.status_code, resp.text)

Try / catch

try:
    configure_agentops_otel(api_key=key)
except (RuntimeError, httpx.HTTPError) as e:
    logger.warning('AgentOps tracing disabled (auth failed): %s', e)
    # proceed without tracing; do not crash the app on telemetry setup

Prevention

When it happens

Trigger: Enabling the agentops OTEL exporter (e.g. via litellm's otel presets / AGENTOPS_API_KEY env) with an invalid or revoked API key (401/403 from the auth endpoint); AgentOps auth service returning 5xx during an outage; a corporate proxy intercepting the POST (407/502); network egress to the auth host blocked by firewall.

Common situations: Enabling AgentOps tracing in the litellm proxy or SDK with a stale key from an old project; CI secrets with trailing newline/whitespace pasted into AGENTOPS_API_KEY; containers in restricted networks that cannot reach the AgentOps auth host; AgentOps incident.

Related errors


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

Appendix: source

Thrown at litellm/integrations/otel/presets/agentops.py:128

            return super().export(spans)

    options: Final = spec.options or {}
    return _LazyAuthAgentOpsExporter(endpoint=spec.endpoint, api_key=options.get("api_key"))


def _fetch_agentops_jwt(api_key: str) -> dict[str, Any]:
    # Own a short-lived client rather than ``_get_httpx_client()``: that returns
    # a process-wide cached ``HTTPHandler`` whose connection pool is shared by
    # every caller, so closing it here would break concurrent/subsequent
    # requests. This one-shot auth call gets its own client to close.
    with httpx.Client(timeout=10) as client:
        response: Final = client.post(
            url=_AGENTOPS_AUTH_ENDPOINT,
            headers={"Content-Type": "application/json", "Connection": "keep-alive"},
            json={"api_key": api_key},
        )
        if response.status_code != 200:
            raise RuntimeError(f"Failed to fetch AgentOps token: {response.text}")
        return response.json()


register_exporter_factory(_AGENTOPS_EXPORTER_KIND, _build_agentops_exporter)

View on GitHub (pinned to 77b7c6c40c)