Pointing Claude Code at a self-hosted endpoint
Three environment variables redirect the client. A fourth, less known, prevents errors that are hard to diagnose.
Claude Code accepts any Messages-API-compatible endpoint via ANTHROPIC_BASE_URL, with ANTHROPIC_AUTH_TOKEN for authentication and ANTHROPIC_MODEL to name the model. That's the foundation, and it fits in three lines of configuration.
What that configuration does not solve: the protocol has no field through which a server declares its context window size. The client counts tokens itself from the usage block of each response, and compares that total against a window it infers from the model identifier. Faced with an identifier it doesn't recognize, it assumes one — rarely the right one.
The symptom is unpleasant: instead of compacting the conversation cleanly before hitting the limit, the client overshoots it and gets an error. The user loses a turn with no way to understand why.
The CLAUDE_CODE_MAX_CONTEXT_TOKENS variable declares the window the client should assume. It applies directly when the model identifier does not start with claude- and carries no extended-context suffix — the case for any self-hosted deployment under its own name. One trap worth knowing: naming your model claude-something disables the variable.
The compaction threshold is set separately with CLAUDE_CODE_AUTO_COMPACT_WINDOW, which accepts only a plain token count. The client caps it at the declared window anyway, so the two variables should be read together.