Significant-Gravitas/AutoGPT · error · RuntimeError

Invalid Replicate API token

Error message

Invalid Replicate API token

What it means

RuntimeError translated from a ReplicateError with HTTP status 401 inside generate_agent_image_v1(). Replicate's API rejects the bearer token used by ReplicateClient — the key configured in settings.secrets.replicate_api_key is invalid, expired, or revoked.

Source

Thrown at autogpt_platform/backend/backend/api/features/store/image_gen.py:173

                else:
                    # If it's a URL string, fetch the image bytes
                    result_url = output[0]
                    response = await Requests().get(result_url)
                    image_bytes = response.content
            elif isinstance(output, FileOutput):
                image_bytes = output.read()
            elif isinstance(output, str):
                # Output is a URL
                response = await Requests().get(output)
                image_bytes = response.content
            else:
                raise RuntimeError("Unexpected output format from the model.")

            return io.BytesIO(image_bytes)

        except ReplicateError as e:
            if e.status == 401:
                raise RuntimeError("Invalid Replicate API token") from e
            raise RuntimeError(f"Replicate API error: {str(e)}") from e

    except Exception as e:
        logger.exception("Failed to generate agent image")
        raise RuntimeError(f"Image generation failed: {str(e)}")

View on GitHub (pinned to 9c8bb5550f)

Solutions

  1. Verify the token atReplicate: `curl -H "Authorization: Bearer $REPLICATE_API_KEY" https://api.replicate.com/v1/account` should return 200.
  2. If invalid, generate a new API token in Replicate account settings and update backend secrets/.env.
  3. Redeploy/restart the backend so Settings() picks up the corrected value.
Defensive patterns

Strategy: validation

Validate before calling

import httpx

async def replicate_token_valid(key: str) -> bool:
    async with httpx.AsyncClient() as client:
        r = await client.get(
            "https://api.replicate.com/v1/account",
            headers={"Authorization": f"Bearer {key}"},
        )
        return r.status_code == 200

Try / catch

try:
    image = await generate_agent_image_v1(agent)
except RuntimeError as e:
    if "Invalid Replicate API token" in str(e):
        raise HTTPException(503, "Image provider credentials rejected") from e
    raise

Prevention

When it happens

Trigger: Agent-image generation where the configured REPLICATE_API_KEY is wrong (typo, truncated when copied, deleted from the Replicate account, or an environment placeholder never replaced). The 401 surfaces on the first prediction request.

Common situations: Rotated/revoked token not updated in deployment secrets; placeholder values from .env.default leaking to staging; key copied with whitespace or quotes.

Related errors


AI-assisted analysis of Significant-Gravitas/AutoGPT@9c8bb5550f (2026-08-14). Data as JSON: /api/errors/801b02a715191406. Report an issue: GitHub.