Seven-forty, and the reply pile keeps growing. Children write back and forth all evening, and the sorting desk hands each reply to whichever elf looks right, who starts from a blank page.
- Santa
Little Tim wrote three times tonight and got three different elves. One rhymed, one wrote an essay, one called him Timothy!
- Sprocket
Easy! I'll send every letter straight to Tinsel. Same elf every time!
- Santa
My head elf, answering "is it Christmas yet?" all night? I've seen what head-elf hours cost.
- Tinsel
Flattered, but no. Turn on session affinity and send a thread id: Tim's first letter picks the elf, the rest go to the one with his file open.
- clientYour appthread.id: letter-tim-42
- smart routerSession affinityttl_seconds: 600
- 1miss2hitturn 1Score and pinlater turnsPinned modelsame model · warm cache
How it works
Tinsel’s fix is one setting on the router and one field on each request. A Smart Router normally scores every request and picks a model for it. In a chat, that means each turn can land on a different model, which resets the provider prompt cache. Session affinity stops that:
- Seed. Your app sends a thread id on every turn:
thread.idin the body or theX-ORQ-THREAD-IDheader. On/v3/router/responses, aconversation.idworks when there is no thread id. When body and header both carry one, the body wins. - Miss. The first turn is scored and routed as usual. The pin is written only after that turn’s first provider call succeeds, so a failed first turn never pins a model.
- Hit. Later turns go straight to the pinned model, with no scoring call on the request path. Each hit refreshes the idle window.
- Expire. A thread that stays quiet longer than
ttl_secondsis routed fresh on its next turn. There is no absolute ceiling while it stays active.
A pin holds a model name, a band and a timestamp. No message content is stored with it.
Try it with curl
Turn it on for an existing router:
curl -X PATCH https://my.orq.ai/v2/smart-routers/$SMART_ROUTER_ID \
-H "Authorization: Bearer $ORQ_API_KEY" \
-H "Content-Type: application/json" \
-d '{"session_affinity": {"enabled": true, "ttl_seconds": 600}}'
Or with the CLI: orq smart-routers update $SMART_ROUTER_ID --session-affinity '{"enabled": true, "ttl_seconds": 600}'.
Or in the UI: open the Smart Router in AI Gateway, turn on Pin conversations to one model and set Pin for to 1, 5, 10, 30 or 60 minutes. The default is 10.
Replace workspace_name@orq/elf-desk in the examples below with your router’s reference, <workspacename>@orq/<name>.
Then send two turns on one thread and print the header:
URL=https://my.orq.ai/v3/router/chat/completions
for turn in "Is my sled ready?" "Can it be blue?"; do
curl -s -D - -o /dev/null -X POST "$URL" \
-H "Authorization: Bearer $ORQ_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"workspace_name@orq/elf-desk\",
\"messages\": [{\"role\": \"user\", \"content\": \"$turn\"}],
\"thread\": {\"id\": \"letter-tim-42\"}
}" | grep -i x-orq-session-affinity
done
The first line says miss, the second says hit. The thread id only labels the calls: a real chat app still sends the earlier messages itself.
If your client cannot add body fields, send the X-ORQ-THREAD-ID header instead.
Or from the OpenAI SDK
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["ORQ_API_KEY"],
base_url="https://my.orq.ai/v3/router",
)
for content in ["Is my sled ready?", "Can it be blue?"]:
raw = client.chat.completions.with_raw_response.create(
model="workspace_name@orq/elf-desk",
messages=[{"role": "user", "content": content}],
extra_body={"thread": {"id": "letter-tim-42"}},
)
print(
raw.headers.get("x-orq-session-affinity"),
raw.parse().model,
)
The model field names the concrete model on both turns.
When to use it
- Chat assistants where turns share a long system prompt or tool set, so the provider prompt cache pays off.
- Agent loops that call the router several times per user turn and should not change models halfway.
- Support conversations where tone and level of detail should stay the same from first reply to last.
When a pin is dropped
The router drops the pin and routes the turn fresh when the pinned model fails on the turn’s first provider call, leaves the pool, or is no longer callable. It also drops a pin it cannot parse, and ignores the pin for one turn when the pin store cannot be read. A dropped pin never fails the request.
Agents whose primary model is a Smart Router use session affinity too, but return no header. Check the orq.session_affinity.hit span attribute in traces instead.
Nice list
- One thread id per conversation, sent on every turn. The pin is keyed on that id, never on the message text.
- Set
ttl_secondsto how long a user may pause: 60 to 3600, 600 when left out. Every hit restarts the clock. - Call the Smart Router directly. Pins are per router, so a load balancer over several routers reshuffles requests every turn.
- Start each thread with a foreground request. Responses sent with
background: truereuse a pin but never write one.
Naughty list
- Generating a fresh thread id on every request. Each turn is then the first turn of a new thread: a miss, scored and routed again.
- PATCHing
{"enabled": true}to flip the switch and expecting your 30-minute window to stay. The object is replaced whole, sottl_secondsresets to 600. - Switching API keys halfway through a conversation. Pins are per API key and identity, so the new key gets a fresh pin.
- Seeding some turns with
thread.idand others withconversation.idof the same value and expecting one pin. They make two independent pins.
Elf's challenge
Send three turns to your Smart Router: two with thread.id letter-tim-42, then one with letter-tim-43. What does X-Orq-Session-Affinity say on each turn?
Stuck? Ask Tinsel for a hint
Turns 1 and 3 say miss: each is the first turn of its own thread. Turn 2 says hit, with the same concrete model in model as turn 1. If turn 2 also says miss, turn 1 may have written no pin: for example, its first provider call failed, or the scorer could not classify it with confidence.
Christmas crackerWhy doesn't the letter desk use cookies for sticky sessions?Pull it for the punchline
Santa keeps eating them. So they pin on the thread id.