Per-user sessions (self-hosted proxy)
The Chat Completions API continues a conversation when you send
back metadata.session_id. If you would rather not track session ids at all —
for example you have many end users behind one integration — you can deploy a
small self-hosted proxy that does it for you: pass a stable user string and
each user gets one rolling session, resumed automatically.
The proxy is a Cloudflare Worker that ships in the Manyfold repository under
apps/chat-session-proxy. It sits in front of /api/v1/chat/completions, maps
user to a session, and is otherwise a transparent passthrough. The core API
and your API tokens are unchanged.
Use it
Point your OpenAI-compatible client at the deployed Worker and add user:
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.MANYFOLD_API_TOKEN,
baseURL: 'https://<your-worker>.workers.dev/v1'
})
await client.chat.completions.create({
model: 'agt_your_agent_id',
user: 'end-user-123',
messages: [{ role: 'user', content: 'Continue from last time.' }]
})
Call again with the same user and the conversation resumes — no session id to
store. If you do send metadata.session_id yourself, that takes precedence.
Deploy
See apps/chat-session-proxy/README.md in the Manyfold repository for the full
setup: create a Workers KV namespace, set your upstream origin, then
wrangler deploy. The folder is self-contained and can be copied out and
deployed on its own.
Notes
- Your API token is forwarded to Manyfold unchanged; the proxy stores no secret.
- Sessions are namespaced per token and per agent, so the same
usertalking to different agents stays in separate conversations. - The mapping is stored in Workers KV (eventually consistent). For strict per-user serialization, the README explains swapping in a Durable Object.