# Redirecting an agent's model endpoint to a local port

Address: https://nowa.im/cotty/research/field-notes/2026-11-model-endpoint-redirect/  
Created: 2026-11-22

On 2026-11-22, Claude Code was started with its model endpoint pointed at a port on the same machine, and a local program received the connection. This is a first-hand observation from a working session. It verifies that redirection works. It does not verify that a local proxy can read or redact the requests.

## What was done

- **Situation.** An API connection error with Microsoft Foundry was being investigated. The aim was to see the TLS ClientHello that Claude Code sends.
- **Setup.**
  - `ANTHROPIC_FOUNDRY_BASE_URL=https://127.0.0.1:18443/anthropic`.
  - `CLAUDE_CODE_MAX_RETRIES=0`, and a fake API key. No real key was used.
  - The command was `timeout 30 claude -p "hi"`.
  - Claude Code 2.1.330 was installed. The version at the time of the capture was not recorded separately, and was most likely the same.
- **Listener.** A short Python script, kept only as a temporary file. It accepts one TCP connection on 127.0.0.1, reads the first TLS record, parses the ClientHello and closes the connection. It does not complete the TLS handshake.
- **Result.** At about 19:28 (UTC+8) the connection arrived. The ClientHello record was 1477 bytes, plus the 5-byte record header. Its key shares were X25519MLKEM768 and X25519.

## Findings

- Claude Code connects to the address that the base URL names, including a port on the loopback interface.
- A local program can receive that connection. So a terminal that starts the agent can put a proxy in the path, and the agent does not have to cooperate.

## What this means for cotty

This supports the launch route in [Capabilities](https://nowa.im/cotty/product/capabilities/): start the agent with its model endpoint pointed at a local proxy. Only the redirect is verified. The next test is a proxy that ends the TLS connection, reads a request and forwards it.

## Not covered

- **Reading the request.** The listener never finished the handshake, so no request content was seen.
- **Ending TLS in the proxy.** There are two ways, and neither was tried:
  - Write the base URL with `http://`. It is not known whether Claude Code accepts a plain local address.
  - Give the proxy a certificate that the client trusts, for example through `NODE_EXTRA_CA_CERTS`.
- **Other agents and other settings.** Only Claude Code with the Foundry setting was tried. `ANTHROPIC_BASE_URL`, and Codex with `OPENAI_BASE_URL`, were not.
