shmem gateway serve
Receives OTLP access logs from an agent gateway and stores them as memories, capturing agent traffic centrally instead of installing a hook on every machine.
shmem gateway serve # listen on 127.0.0.1:4318
shmem gateway serve --addr 0.0.0.0:4318 --token "$T" # accept remote exporters
shmem gateway serve --project acme-api # force a project tag
| Flag | Description | Default |
|---|---|---|
--addr |
Address to listen on | 127.0.0.1:4318 |
--path |
OTLP logs path | /v1/logs |
--token |
Required bearer token (else $SHMEM_INGEST_TOKEN); unauthenticated if unset |
— |
-p, --project |
Force a project tag on every ingested fact | — |
--include-system |
Store system-role prompt messages too | off |
--verbose |
Log every stored fact | off |
The gateway accepts OTLP/HTTP protobuf only (Content-Type: application/x-protobuf, which OTLP exporters send) and refuses requests from a browser (any request with an Origin header), so a web page can't write into your memory through it. A project tag carried in a record's attributes is used only when it is a plain project name. Without --token, any program on this machine can still post to it; set a token when other users share the machine.
GET /healthz returns running counts of records, facts, duplicates and
skips.
Point agentgateway at it — no OpenTelemetry Collector is needed, though one still works if you already fan out to other backends:
frontendPolicies:
accessLog:
otlp:
host: shmem-ingest.internal:4318
protocol: http
path: /v1/logs
add:
shmem_identity: 'coalesce(apiKey.user, apiKey.user_id, apiKey.owner, apiKey.name, jwt.sub, jwt.email, basicAuth.username, "")'
shmem_team: 'coalesce(apiKey.group, apiKey.team, jwt.groups[0], "")'
shmem_project: 'coalesce(request.headers["x-shmem-project"], apiKey.project, jwt.project, "")'
shmem_client: 'coalesce(request.headers["x-title"], request.headers["user-agent"], "")'
shmem_tool: 'mcp.tool.name'
shmem_tool_args: 'mcp.tool.arguments'
shmem_tool_res: 'mcp.tool.result'
shmem_prompt: 'llm.prompt' # content tier — opt in per route
shmem_completion: 'llm.completion'
The identity expression coalesces deliberately rather than reading
apiKey.user alone. Deployments differ in where they put identity, and
agentgateway's own default chain misses some of them — Solo's reference
Helm chart, for instance, issues one key per team carrying
metadata.user_id, which is not in that chain. coalesce and default are
built-in CEL functions, so unset fields fall through instead of erroring.
On Kubernetes the same fields go in an AgentgatewayPolicy, which takes a
list rather than a map:
spec:
frontend:
accessLog:
attributes:
add:
- name: shmem_identity
expression: coalesce(apiKey.user, jwt.sub, jwt.email, "")
Tagging traffic with a project
A gateway knows who is calling, never which codebase — so the project tag
has to come from the client, which does know. Every major agent runtime can
attach a custom header when pointed at a gateway, and
request.headers["x-shmem-project"] above picks it up.
Claude Code reads ANTHROPIC_CUSTOM_HEADERS, and honors it from a project
settings file — so the tag lives in the repo it describes, restoring the
directory-to-project mapping that hooks get from cwd:
// .claude/settings.json, committed alongside the code
{
"env": {
"ANTHROPIC_BASE_URL": "https://llm-gateway.example.com",
"ANTHROPIC_CUSTOM_HEADERS": "X-Shmem-Project: acme-api"
}
}
opencode takes the same idea in its provider config:
{
"provider": {
"gateway": {
"options": { "baseURL": "https://llm-gateway.example.com/v1" },
"headers": { "X-Shmem-Project": "acme-api" }
}
}
}
Clients routed through OpenRouter already send HTTP-Referer and X-Title
app-attribution headers; shmem_client above captures the latter, which
identifies the tool (opencode, Continue) rather than the project. Note
that agentgateway reaches OpenRouter as an OpenAI-compatible backend, so
llm.provider reports openAI — read gw.endpoint to tell them apart.
Facts arrive untagged when no header is set, which is the intended behaviour: an untagged fact is honest, a guessed one is misfiled.
Notes:
- Identity is the gateway's.
shmem_identitybecomes the fact's caller and owner,shmem_teamits group, and every other attribute is kept under agw.*metadata key. Attach the auth policy to every route you want attributed — including the LLM route, which is easy to miss and is the one carrying the conversations. - Projects are asserted, never inferred. A gateway knows who is calling
and where in the network, never which codebase. If a fact should carry a
project, put it in API-key or JWT metadata and map it to
shmem_project; otherwise facts are stored untagged. - Re-sent turns collapse. Every agent call re-sends the whole conversation, so messages are keyed on content and stored once, dated when first seen.
- shmem ignores its own MCP tools, so memory reads and writes made through the same gateway are not re-recorded as facts.
- Content only arrives if your CEL bundle exports it. The default tier captures models, tokens and tool names but no conversation text.