Tutorial: run Claude Code on a local model

This guide is for someone new to token-free. You will:

  1. build token-free;
  2. start a local model server;
  3. use that server from Claude Code;
  4. optionally, put token-free's private plane in front of the server for other clients.

Parts 1 to 3 need no provider API key, no Claude subscription and no sudo.

Read this first: what works today

There are three programs, and each listens on its own port:

Program Address in this guide Role
llama-server (the engine) 127.0.0.1:8081 Loads the model and generates text. It is not part of token-free.
tf-private (the private plane) 127.0.0.1:8788 Forwards only to local or approved LAN engines, under kernel egress rules, and keeps a privacy log.
tf-router (the public plane) 127.0.0.1:8787 Routes non-sensitive requests to cloud providers. It is not needed for a local model.

token-free does not run models itself, so you always need an engine.

Results with Claude Code 2.1.332 and llama.cpp build 10964 on macOS (2026-11-30):

Path curl Claude Code
client -> engine works works, including a Read tool call
client -> tf-private -> engine works fails: 404
client -> tf-router -> engine (test provider) works fails: 400

Until these are fixed, connect Claude Code directly to the engine (Part 3). On that path token-free does not label requests, restrict egress or write a privacy log. Model requests go to your machine, but Claude Code's tools (shell commands, web access, MCP servers) can still use the network. Do not treat Part 3 as network isolation.

Part 1: Install and build

You need macOS or Linux, about 3 GB of free disk space for the example model, and enough free memory to load it. The commands below were tested on macOS.

Step 1. Install the tools

Check that all three are available:

cargo --version
llama-server --version
claude --version

Step 2. Build token-free

Run this from the root of your token-free checkout:

cargo build -p tf-cli -p tf-private -p tf-router
ls target/debug/tf target/debug/tf-private target/debug/tf-router

Part 3 does not use these programs. Part 4 and the optional CLI section do.

Part 2: Start the local model server

Step 3. Check that the port is free

This guide uses port 8081 because 8080 is often taken:

lsof -nP -iTCP:8081 -sTCP:LISTEN

No output means the port is free. If it is taken, choose another port and use it everywhere this guide says 8081.

Step 4. Start the engine

In terminal 1, run the command below and leave it running. The first run downloads the model (about 2.5 GB), so it needs internet access:

llama-server -hf Qwen/Qwen3-4B-GGUF:Q4_K_M --alias qwen3-4b \
  --host 127.0.0.1 --port 8081 -c 40960 -np 1 --jinja

If you already have a GGUF file, replace -hf Qwen/Qwen3-4B-GGUF:Q4_K_M with -m /path/to/model.gguf.

Option Why
--alias qwen3-4b The model name that clients use.
--host 127.0.0.1 Accepts connections only from this machine. Do not use 0.0.0.0: this server has no password.
-c 40960 The context window in tokens. This is the most this model supports.
-np 1 One request slot, which gets the whole context window.
--jinja Uses the model's chat template, which tool calls need.

Qwen3-4B is small. Use it to check your setup, not for serious coding. For real work, use a larger model that supports tool calls and fits in memory. If you change models, update the alias and context size in Step 6 too.

Step 5. Test the engine

In terminal 2, check that the model has loaded:

curl -s http://127.0.0.1:8081/health

Wait until it prints {"status":"ok"}. Then check the actual context size:

curl -s http://127.0.0.1:8081/props |
  python3 -c 'import json,sys; print(json.load(sys.stdin)["default_generation_settings"]["n_ctx"])'

This should print 40960. Use the number printed here, not the one you asked for. In our test, asking for 65536 still gave 40960.

Now send a message in the format Claude Code uses. Keep the quotes around the URL: without them, zsh treats ? as a wildcard.

curl -s 'http://127.0.0.1:8081/v1/messages?beta=true' \
  -H 'content-type: application/json' \
  -H 'anthropic-version: 2023-06-01' \
  -d '{"model":"qwen3-4b","max_tokens":300,"messages":[{"role":"user","content":"Reply with one word: pong /no_think"}]}'

The reply should contain "type":"message" and text such as pong. If you get a 404, update llama.cpp.

Part 3: Use the server from Claude Code

Step 6. Create a launcher

A separate launcher leaves your normal claude command unchanged. Create it with:

mkdir -p ~/.local/bin
cat > ~/.local/bin/claude-local <<'EOF'
#!/bin/sh
unset ANTHROPIC_API_KEY
export CLAUDE_CONFIG_DIR="$HOME/.claude-local"
export ANTHROPIC_BASE_URL="http://127.0.0.1:8081"
export ANTHROPIC_AUTH_TOKEN="local"
export ANTHROPIC_MODEL="qwen3-4b"
export ANTHROPIC_DEFAULT_OPUS_MODEL="qwen3-4b"
export ANTHROPIC_DEFAULT_SONNET_MODEL="qwen3-4b"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="qwen3-4b"
export CLAUDE_CODE_MAX_CONTEXT_TOKENS=40960
export CLAUDE_CODE_MAX_OUTPUT_TOKENS=8192
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1
exec claude "$@"
EOF
chmod +x ~/.local/bin/claude-local
Setting Why
ANTHROPIC_BASE_URL The engine's address. Do not add /v1; Claude Code adds it.
ANTHROPIC_AUTH_TOKEN A placeholder. Our engine does not check it. Do not put a real key here.
ANTHROPIC_MODEL and ANTHROPIC_DEFAULT_*_MODEL Make the main model and the Opus, Sonnet and Haiku aliases use the local model. The value must match --alias.
CLAUDE_CODE_MAX_CONTEXT_TOKENS The number from Step 5. Without it, Claude Code 2.1.332 assumes 1 million tokens for this model.
CLAUDE_CODE_MAX_OUTPUT_TOKENS Leaves more of the small window for input. Claude Code asked for 32000 output tokens without it.
CLAUDE_CONFIG_DIR Keeps these settings and sessions apart from your normal ones. Your usual user-level plugins and hooks are not loaded. A project's own .claude settings can still apply.
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC Turns off nonessential Claude Code traffic. It does not block network access.

Step 7. Run a test that uses a tool

This checks that the model can call a tool, not just chat:

mkdir -p ~/token-free-demo
cd ~/token-free-demo
echo 'The secret word is persimmon.' > hello.txt
~/.local/bin/claude-local -p "Read hello.txt and tell me the secret word." --allowedTools Read </dev/null

The answer should include persimmon. An unrecognized_model line is expected, because qwen3-4b is not a Claude model.

The first reply can be slow. Claude Code sends a lot of instructions and tool definitions: our requests reached about 12,000 tokens, and the test took 18 seconds on an M1 Ultra.

Step 8. Start an interactive session

cd ~/token-free-demo
~/.local/bin/claude-local

On the first run:

  1. Choose a theme.
  2. Read the security notes and press Enter.
  3. If you trust the folder, select Yes, I trust this folder. No, exit is selected by default, so pressing Enter straight away closes Claude Code.

Our test did not ask for a login. In the session, run /status to check the model and base URL, then try:

Read hello.txt and tell me the secret word.

Check each action before you approve it, especially with a small model.

Step 9. Stop, or go back to normal

Part 4 (optional): Use the private plane with other clients

Claude Code does not work through tf-private yet (see the table at the top). This part is for curl and other clients that post to exactly /v1/messages or /v1/chat/completions. tf-private does not translate between the two, so the engine must support the one you use.

These steps are for macOS and need administrator rights. They add a hidden _tfpriv user, firewall rules and two background services. The exact sudo commands below were not run for this tutorial. We did check the install dry run and the tf-private request and privacy log. That request ran under Seatbelt as a normal user, so it did not test the firewall rules for _tfpriv. On Linux, the user is tfpriv, and the rules use nftables.

Step 10. Preview and install the egress rules

If token-free is already installed on this machine, find out which LAN hosts it uses before you continue: the install replaces the installed ACL. From the repository root, create an ACL that allows no LAN hosts, then preview the changes:

echo '# loopback only' > /tmp/token-free-acl
TF_PRIVATE="$PWD/target/debug/tf-private" sh scripts/confine/install.sh --dry-run /tmp/token-free-acl

Always pass your ACL file: without one, the script uses an example that allows 10.0.0.21:8443. Read the preview. If you agree with it, install and check the status:

sudo env TF_PRIVATE="$PWD/target/debug/tf-private" sh scripts/confine/install.sh /tmp/token-free-acl
cat /var/run/tokenfree/attest.json

Continue only if the status contains "ok":true.

Step 11. Configure and start the private plane

Create a log folder that _tfpriv can write to, then the configuration:

sudo install -d -o _tfpriv -m 0700 /var/db/tokenfree-audit
sudo tee /usr/local/etc/tokenfree/private.toml >/dev/null <<'EOF'
listen = "127.0.0.1:8788"
user = "_tfpriv"
acl_file = "/usr/local/etc/tokenfree/acl"
audit_log = "/var/db/tokenfree-audit/privacy.jsonl"

[[route]]
model = "local/qwen3-4b"
addr = "127.0.0.1:8081"
upstream_model = "qwen3-4b"
EOF

Clients ask for local/qwen3-4b, and tf-private sends qwen3-4b to the engine. With the engine still running, start the private plane in terminal 3. Run it as _tfpriv, not as root:

cd /
sudo -u _tfpriv /usr/local/libexec/tokenfree/tf-private serve \
  --config /usr/local/etc/tokenfree/private.toml

It should print tf-private: serving on 127.0.0.1:8788 as _tfpriv.

Step 12. Send a request and check the privacy log

In terminal 2:

curl -s http://127.0.0.1:8788/v1/messages \
  -H 'content-type: application/json' \
  -H 'anthropic-version: 2023-06-01' \
  -d '{"model":"local/qwen3-4b","max_tokens":300,"messages":[{"role":"user","content":"Reply with one word: pong /no_think"}]}'

sudo /usr/local/libexec/tokenfree/tf-private audit-privacy \
  --log /var/db/tokenfree-audit/privacy.jsonl --acl /usr/local/etc/tokenfree/acl

The log entry should include sensitive, local:127.0.0.1:8081 and ok. It records where the request went, not the prompt or the reply.

These rules apply only to processes running as _tfpriv. They do not restrict the engine or Claude Code, which run as your own user.

Press Ctrl-C in terminal 3 to stop tf-private. The rules and background services stay installed. If you installed them only for this tutorial, preview their removal with sh scripts/confine/uninstall.sh --dry-run, then run sudo sh scripts/confine/uninstall.sh.

Optional: Try the tf command

You do not need this for a local model. tf manages the encrypted store that holds cloud provider keys. Run these commands from the repository root. They create a separate store with its own master key, so run the block only once:

TFDIR="$HOME/.token-free-demo"
mkdir -m 700 "$TFDIR"
(umask 077; openssl rand -hex 32 > "$TFDIR/master.key")
export TF_MASTER_KEY="$(cat "$TFDIR/master.key")"
target/debug/tf --db "$TFDIR/tf.db" init
target/debug/tf --db "$TFDIR/tf.db" provider list
target/debug/tf --db "$TFDIR/tf.db" doctor

Run unset TF_MASTER_KEY when you finish.

Troubleshooting

Problem What to do
Connection refused on 8081 Start the engine (Step 4) and wait for /health to report ok.
The engine cannot use its port Choose another port and change it in claude-local too (and in private.toml if you did Part 4).
zsh: no matches found Put quotes around any URL that contains ?.
Claude Code says there is an issue with the selected model Check that ANTHROPIC_BASE_URL uses port 8081, not 8788, and that the model name matches --alias.
unsupported role Some("system") Claude Code is connected to tf-router (8787). Use the engine (8081).
Claude Code assumes 1 million tokens, or the prompt is too long Set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the number from Step 5, then start a new session.
The model replies but does not use tools Check that the engine was started with --jinja, and try a larger model that supports tool calls.
must run as _tfpriv Start tf-private with sudo -u _tfpriv (Step 11).
kernel egress filter not active Check /var/run/tokenfree/attest.json and the install in Step 10. Once fixed, restart tf-private. Do not change the canary address to get past this check.
tf-private returns 503 Read the error message. Check that the engine is running, the model name matches a route, and the egress rules are active.
tf status says router not running, but the router is running Use the same --db and --control-socket for both. Also try a shorter path: a 105-character socket path failed on macOS.

Learn more