xtekky/gpt4free · error · MissingAuthError

Claude cookie not found. Please set the 'CLAUDE_COOKIE' envi

Error message

Claude cookie not found. Please set the 'CLAUDE_COOKIE' environment variable.

What it means

MissingAuthError from g4f/Provider/needs_auth/Claude.py raised during create_async_generator: the provider needs a Claude (claude.ai) cookie string to authenticate, resolved from the CLAUDE_COOKIE env var, the caller-supplied api_key, or a cookie jar for the provider's cookie_domain. If none yields a value, the request cannot carry authentication, so it fails before any network call.

Source

Thrown at g4f/Provider/needs_auth/Claude.py:36

    organization_id = None
    cookie_domain = "claude.ai"

    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: Messages,
        api_key: str = None,
        cookies: list = None,
        base_url: str = base_url,
        **kwargs,
    ) -> AsyncResult:
        api_key = os.environ.get("CLAUDE_COOKIE", api_key)
        cookies = cookies or get_cookies(cls.cookie_domain)
        if not api_key:
            api_key = "; ".join([f"{key}={value}" for key, value in cookies.items()])
        if not api_key:
            raise MissingAuthError(
                "Claude cookie not found. Please set the 'CLAUDE_COOKIE' environment variable."
            )
        if not cls.organization_id:
            cls.organization_id = os.environ.get("CLAUDE_ORGANIZATION_ID")
            if not cls.organization_id:
                cls.organization_id = cookies.get("lastActiveOrg")
            if not cls.organization_id:
                raise MissingAuthError(
                    "Claude organization ID not found. Please set the 'CLAUDE_ORGANIZATION_ID' environment variable."
                )
        async for chunk in super().create_async_generator(
            model=model,
            messages=messages,
            base_url=f"{base_url}/{cls.organization_id}",
            headers={"cookie": api_key},
            **kwargs,
        ):
            yield chunk

View on GitHub (pinned to 973504e177)

Solutions

  1. Export the claude.ai cookie header from a logged-in browser session and set it: export CLAUDE_COOKIE='cookieKey=value; otherKey=value'.
  2. Alternatively place a claude.ai HAR/cookies file in g4f's har_and_cookies directory so get_cookies(cls.cookie_domain) returns entries.
  3. Alternatively pass api_key="<cookie header string>" when calling the provider.
  4. Verify the cookie domain in your export matches the provider's cls.cookie_domain.

Example fix

# before
# no env var, no cookies -> MissingAuthError
resp = await client.chat.completions.create(model="claude-...", provider=Claude, messages=msgs)

# after
import os
os.environ["CLAUDE_COOKIE"] = "sessionKey=...; otherKey=..."
resp = await client.chat.completions.create(model="claude-...", provider=Claude, messages=msgs)
Defensive patterns

Strategy: validation

Validate before calling

import os

def claude_cookie_configured():
    return bool(os.environ.get("CLAUDE_COOKIE"))

Try / catch

try:
    resp = await client.chat.completions.create(model=m, provider=Claude, messages=msgs)
except MissingAuthError as e:
    if "CLAUDE_COOKIE" in str(e):
        os.environ["CLAUDE_COOKIE"] = fetch_fresh_cookie_header()
        resp = await client.chat.completions.create(model=m, provider=Claude, messages=msgs)

Prevention

When it happens

Trigger: Calling Claude via g4f with no CLAUDE_COOKIE environment variable, no api_key argument, and no exported claude.ai cookies in g4f's cookie storage. The code builds api_key from env, then falls back to joining jar cookies; if both are empty the error is raised.

Common situations: New machine/container without CLAUDE_COOKIE set; cookies exported but for the wrong domain so the jar is empty; CI environments that never had the var configured.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/8bdf661757bb1de7. Report an issue: GitHub.