Your Tor client doesn't wait for you to type a URL before it starts working. Within a few seconds of bootstrapping it has usually built several circuits in advance, so that when a request does arrive there's one ready to go. Most of what follows happens before you've clicked anything.
Step one: picking the path
A normal exit circuit has three relays: a guard, a middle and an exit. The client picks them from the latest consensus (see directory authorities and the consensus) and it picks them in a specific order. The exit comes first, because it's the most constrained: it has to allow connections to the port you need. Then the guard, which is almost always your existing long-term guard. Then a middle relay to fill the gap.
Selection is weighted by bandwidth. A relay that the network has measured as fast gets picked proportionally more often than a slow one, and there are extra position weights so that scarce guard and exit capacity isn't wasted on middle positions. That's why a relay with the Exit flag carries few middle-position circuits when exit bandwidth is tight.
There are also rules about which relays may share a circuit:
- No relay may appear twice.
- No two relays may be in the same declared relay family.
- No two relays may be in the same IPv4 /16 network (configurable with
EnforceDistinctSubnets, which you should leave alone). - Every relay must have the Running and Valid flags, and the exit must not carry BadExit.
The family and subnet rules are there because one operator, or one hosting provider, controlling two hops of your path is a lot closer to controlling both ends than it should be.
Step two: the first hop
The client opens a TLS connection to the guard, if it doesn't have one already. Inside that TLS link the two sides exchange VERSIONS, CERTS and NETINFO cells to agree on a link protocol version and confirm the guard's identity keys match what the consensus promised.
Then the client sends a CREATE2 cell. It contains a fresh circuit ID that the client picked and the first half of an ntor handshake. The guard answers with CREATED2, carrying its half. Both sides now share keys for this hop. This takes one round trip.
Step three: extending
To add the middle relay, the client doesn't connect to it directly. It sends an EXTEND2 relay cell down the existing circuit, encrypted to the guard. The cell says "connect to this relay, at this address, with this identity, and pass along this handshake". The guard opens (or reuses) a TLS connection to the middle, sends it a CREATE2 with the handshake data, gets back CREATED2, and wraps the reply in an EXTENDED2 cell back to the client.
Extending to the exit is the same thing one hop further: an EXTEND2 encrypted to the middle, which travels through the guard as opaque data. The guard never learns who the exit is.
The EXTEND2 cells travel as RELAY_EARLY cells rather than ordinary RELAY cells. Relays only allow a limited number of RELAY_EARLY cells per circuit (eight), which caps how long a circuit can get. That limit exists so nobody can build absurdly long circuits through the network to amplify load. RELAY_EARLY also has a history: in 2014 researchers from Carnegie Mellon abused it as a covert signal to tag onion service lookups, which we've written up in the relay early attack. Relays now refuse RELAY_EARLY in the inbound direction.
Three hops, three round trips, each one longer than the last. On a typical connection that adds up to somewhere around a second, which is why building circuits ahead of time matters.
Timeouts and failures
Circuits fail all the time. A relay goes offline between consensuses, or it's overloaded and slow, or a TLS connection dies. Tor handles this by measuring how long circuit builds normally take and fitting a distribution to the results. After it has collected enough data points, it sets CircuitBuildTimeout adaptively, abandoning builds that take much longer than the network normally does. You'll see something like this in the log after bootstrapping:
[notice] Set buildtimeout to low value 2014ms. Setting circuit build timeout to 2.5 seconds ...
The exact message varies by version. The point is that Tor tries not to put you on a relay path that is slower than most of the network. If the network suddenly gets slower everywhere (a common symptom of censorship or a lossy link) Tor notices the spike in timeouts and relaxes the threshold again.
Attaching streams
Once a circuit exists, the client can attach streams to it. A stream is one TCP connection's worth of traffic, started with a RELAY_BEGIN cell that names the destination host and port. The exit resolves the hostname itself (so DNS doesn't leak locally), connects, and replies with RELAY_CONNECTED.
Which streams share a circuit is where stream isolation comes in. Tor's default SOCKS port isolates by SOCKS username and password, among other things. Tor Browser takes advantage of this by sending the first-party domain of the tab as the SOCKS credentials, so everything on example.com uses one circuit and everything on example.org uses another. That's why your circuit display changes from site to site, and why clicking "New Circuit for this Site" only affects one domain. The difference between that and a full New Identity is laid out in New Identity vs New Circuit.
Circuits also age out. After MaxCircuitDirtiness (ten minutes by default) a circuit stops accepting new streams, though anything already running on it carries on. The next request gets a fresh circuit with a new middle and exit and, almost always, the same guard.
Not every circuit is three hops
Directory fetches from your guard use a one-hop circuit with BEGIN_DIR, since the guard already knows who you are and the data is public anyway. Onion service connections use several circuits stitched together through introduction and rendezvous points. And onion services running vanguards use a restricted set of second- and third-hop relays rather than picking middles at random, which is described in Vanguards and Guard Discovery Attacks.
If you want to watch all of this happen, enable the control port and run nyx. Its circuit view shows each build with the relays' nicknames and flags, and it's the fastest way I know to build an intuition for how often circuits come and go.