Why is localhost:4096 used? Is it arbitrary?
This post might have stale content, as dev is 2006 commits ahead.
why is localhost:4096 a thing? is it arbitrary?
Based on my investigation, here's what I found:
##The Port 4096 Choice
It appears to be an arbitrary default choice — there's no comment or documentation explaining why specifically 4096 was chosen. Here's what the code shows:
###From packages/opencode/src/server/server.ts:
// Line 69 - Default URL fallback
export function url(): URL {
return _url ?? new URL("http://localhost:4096")
}
// Line 2879 - Server startup logic
const server = opts.port === 0 ? (tryServe(4096) ?? tryServe(0)) : tryServe(opts.port)
The logic shows:
- If
port === 0is specified, it first tries port 4096, and if that fails, falls back to port 0 (which lets the OS assign an available port) - Otherwise it uses whatever port is explicitly configured
###Why 4096 might have been chosen (speculation):
- Powers of 2 - 4096 is 2^12, which is a common "nice number" in computing
- Above the privileged range - Well above port 1024 (which requires root on Unix systems)
- Unlikely to conflict - Not a commonly used well-known port
- Easy to remember - A round power-of-2 number
But there's no authoritative source in the codebase explaining the choice — it's just the default that was picked.
END OF POST