Guides

Use Claude Code with an LLM gateway

To use Claude Code with a memtrunk gateway, create an API key in the console, set ANTHROPIC_BASE_URL to your memtrunk address with no path, put the key in ANTHROPIC_API_KEY, and run claude. Requests then pass through memtrunk, where they are checked against the key's limits and recorded in the usage and request logs.

This guide covers the setup, how to confirm traffic is reaching memtrunk, and the errors you are most likely to meet. If gateways are new to you, what is an LLM gateway explains the pieces first.

Before you start

You need three things:

  • A memtrunk instance you can sign in to. Your memtrunk address is the origin you open the console at, without any path. The examples below use the placeholder https://memtrunk.your-host.dev. If you are unsure of your address, ask whoever runs your instance.
  • A role that can create API keys. A read-only role cannot create API keys.
  • Claude Code installed and able to run from your terminal.

A note on names: the console calls the credential an "API key". Elsewhere on this site it may be called a caller key. They are the same thing: the key your tools present to memtrunk, as opposed to the provider credentials memtrunk holds behind it.

Step 1: create an API key

  1. Sign in and open API keys in the sidebar. The page lists the keys callers use to authenticate into the proxy; you can create, edit and revoke them.
  2. Create a key. The form asks for a name and a source, and lets you set limits and an expiry:
    • Name helps you recognize the key later, for example the tool or person it belongs to.
    • Source is either "Shared pool" or "Own provider key". It decides which credentials the key draws on.
    • Budget window, Monthly limit (USD), Daily limit (USD) and Monthly token limit set spend caps. Spend caps and budgets for AI coding agents explains how they behave, including what happens when you leave a limit blank.
    • Expires sets a date after which the key stops working.
  3. Copy the key when it is shown. A key is displayed in full when it is created. Its owner can usually reveal it again later, depending on server configuration. memtrunk looks keys up by hash, and you can revoke one at any time.

After creating the key, open the key's details panel and use Test connection. It checks, without making a billed request, that the key is enabled and unexpired, that the Claude provider is enabled for the key and for you, that the monthly limit has not been reached, and that a usable Claude account is available for the key's source. If one of those fails, the message names it. The check is specific to Claude and does not look at daily or token caps.

Step 2: set the environment variables

Claude Code reads its gateway address and credential from environment variables:

bash
export ANTHROPIC_BASE_URL=https://memtrunk.your-host.dev
export ANTHROPIC_API_KEY=<your memtrunk API key>

Use the address with no trailing path. According to Claude Code's gateway documentation, Claude Code adds its own request paths, such as /v1/messages, to the base URL. Some console screens show a longer address for SDK examples; Claude Code should use the bare address shown here.

Claude Code has two credential variables, and they send the key in different headers. According to Claude Code's gateway documentation, ANTHROPIC_AUTH_TOKEN is sent as Authorization: Bearer and ANTHROPIC_API_KEY is sent as x-api-key. memtrunk accepts the key in either header, so either variable works. A few details come from Claude Code's own documentation and may change with the client:

  • In interactive mode, Claude Code asks you once to approve a key set in ANTHROPIC_API_KEY. With ANTHROPIC_AUTH_TOKEN there is no approval prompt and the variable takes effect immediately.
  • A gateway credential variable takes precedence over a saved claude.ai login. The login stays saved and is simply unused while the variable is set.
  • Run /status inside Claude Code to see which credential is active.

You can export the variables in your shell profile, or set them in the env block of Claude Code's settings file, so that every session uses the gateway. See Claude Code's LLM gateway documentation and authentication documentation for the current rules.

Step 3: run Claude Code

Start claude in a project as you normally would. From this point, its model requests go to memtrunk. memtrunk checks the key and its limits, forwards the request to the provider account behind it, and records the usage.

Step 4: confirm requests reach memtrunk

Use any of these checks:

  • Dashboard. Until the first request arrives, the dashboard shows a three-step quickstart: create an API key, point the client at a base URL, and send a request. It notes that usage fills in on its own once requests come through.
  • Usage. The Usage page has Usage and Sessions tabs for every role, plus Teams and Pricing tabs for some administrators. Your key's requests appear there with an estimated cost, calculated from configured prices.
  • Request logs. The Request logs page lists requests. Headers and bodies are redacted by default, and logs are kept for 7 days by default; both are instance settings.
  • A direct request. To test the key without Claude Code, send one request yourself. This is a real request, so it is billed like any other:
bash
curl "$ANTHROPIC_BASE_URL/v1/messages" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "content-type: application/json" \
  -d '{"model":"<model id>","max_tokens":64,"messages":[{"role":"user","content":"Say hello."}]}'

What goes through the gateway

Claude Code talks to memtrunk's Anthropic Messages route, POST /v1/messages. Responses stream as server-sent events when the request asks for streaming. Two neighboring routes are worth knowing about:

  • POST /v1/messages/count_tokens exists, but its result is an approximation based on text length, not Anthropic's own tokenizer.
  • GET /v1/models returns models filtered to the providers the caller is entitled to use.

Alternative: the per-user token URL

Some tools cannot send a key header. For those, memtrunk offers a per-user URL token. Open Profile and find the Proxy connection card, which shows your endpoint and token and lets you rotate the token. The base URL then has the token as its path:

bash
export ANTHROPIC_BASE_URL=https://memtrunk.your-host.dev/<your proxy token>
export ANTHROPIC_AUTH_TOKEN=placeholder

memtrunk does not check the value of ANTHROPIC_AUTH_TOKEN in this setup, because the token in the URL does the authenticating. Setting the variable to any value means Claude Code uses it rather than a saved login, since a credential variable takes precedence over a saved login.

Treat the whole URL like a password, because the token is part of it. If it leaks, rotate it from the same card. Two behaviors differ from API keys:

  • Spend caps do not apply. Limits are attached to API keys. Requests that authenticate with the URL token do not go through the per-key limit check. See the spend caps guide.
  • Different failure modes. An unknown or disabled token returns 404, and repeated misses return 429 with a Retry-After header.

Troubleshooting

What you seeWhat it meansWhat to do
401 "API key required"The request reached memtrunk with no key in a header it reads.Check that the variable is exported in the shell that launches claude, and run /status.
401 "Invalid API key"The key is unknown, disabled or expired, or its owner no longer exists.Check the key's status and expiry on the API keys page, or create a new key.
401 "Account is disabled"The account that owns the key is disabled.Ask an administrator.
429 "Daily spend limit reached"The key's daily USD cap is used up. The response carries a Retry-After header with the seconds until the next UTC midnight.Wait for the reset, or raise the cap.
429 "Monthly spend limit reached" or "Monthly token limit reached"A monthly cap is used up. These responses carry no retry hint.Raise the cap or wait for the next month.
404 on the token URLThe token is unknown or disabled.Copy the current URL from Profile, Proxy connection.
Test connection failsThe message names the failing check, for example "Key is disabled or expired" or "No Claude account available".Fix the named condition and test again.

What this guide does not cover

  • Other clients. Codex CLI has its own routes on the same memtrunk address. This guide does not cover its configuration.
  • Memory. Memory is off by default. Once it is enabled, it works automatically for Claude traffic that goes through the proxy. See Persistent memory for Claude Code and Codex CLI.

For the full capability list, see the features page. For how access control and credential handling work, see security.