Chat History¶
SMG supports multiple storage backends for persisting the conversations, conversation items, and stored responses behind the Responses and Conversations APIs, for analytics, debugging, and compliance.
Overview¶
Multiple Backends¶
Choose from in-memory, PostgreSQL, Redis, or Oracle based on your requirements.
Conversation Tracking¶
Store complete conversation history including messages, tool calls, and reasoning.
Configurable Retention¶
Redis entries expire after --redis-retention-days (default 30 days). The in-memory backend keeps the newest records within count and retention bounds. PostgreSQL and Oracle keep data until it's deleted.
Backend Comparison¶
| Backend | Use Case | Persistence | Scalability |
|---|---|---|---|
memory |
Development, testing | Process lifetime, bounded | Single instance |
none |
Stateless deployments | None | N/A |
postgres |
Production, self-hosted | Durable | High |
redis |
Caching, ephemeral storage | Configurable TTL | High |
oracle |
Enterprise, OCI deployments | Durable | High |
Configuration¶
Backend Selection¶
smg --history-backend <backend> [backend-specific options]| Option | Default | Description |
|---|---|---|
--history-backend |
memory |
Storage backend: memory, none, oracle, postgres, redis |
At startup, SMG contacts the configured PostgreSQL or Redis store once — one connection and one round trip — and refuses to start when the store is unreachable or does not answer within the store's per-call timeout. Once serving, every call to a PostgreSQL or Redis store (waiting for a pooled connection, connecting, and the operation itself) is bounded by --postgres-op-timeout-secs or --redis-op-timeout-secs (default 5 seconds). A call past the bound, or a store that is down, is answered 503 with code storage_unavailable instead of holding the request for as long as the client waits.
Memory Backend¶
The default in-process storage. Suitable for development and testing.
smg --history-backend memory✓ Advantages¶
- Zero configuration
- Fast access
- No external dependencies
✗ Limitations¶
- Data lost on restart
- Not shared across instances
- Records dropped at the count and retention bounds
Configuration Options¶
The records live in the gateway's own memory, so they are bounded. The defaults hold a default deployment at a few hundred MB: a stored response of a short exchange takes about 12 KB, a conversation item about 1.5 KB.
| Option | Default | Description |
|---|---|---|
--history-memory-max-responses |
10000 |
Most stored responses kept; the oldest go first, checked on every write. A background response still running is not dropped by a count bound |
--history-memory-max-items |
100000 |
Most conversation items kept; the oldest go first and leave every conversation that listed them |
--history-memory-retention-secs |
86400 |
How long a record is kept, in seconds; 0 keeps a record until a count bound drops it. A conversation is kept that long after its last use (created, read, or updated) |
None Backend¶
Disables history storage entirely. Use for stateless deployments where persistence isn't needed.
smg --history-backend noneUse when: Privacy requirements prohibit storing conversations, or external systems handle logging.
PostgreSQL Backend¶
Production-ready storage with PostgreSQL.
Configuration Options¶
| Option | Default | Description |
|---|---|---|
--postgres-db-url |
- | PostgreSQL connection URL |
--postgres-pool-max-size |
16 |
Maximum connection pool size |
--postgres-op-timeout-secs |
5 |
Bound in seconds on each store call (pool wait, connect, and the statement); a call past it is answered 503 storage_unavailable. Must be greater than 0 |
Connection URL Format¶
postgres://[user[:password]@]host[:port]/database[?param=value]The scheme can also be postgresql://. SMG requires a host and a database name.
Examples¶
Basic Connection¶
smg --history-backend postgres \
--postgres-db-url "postgres://user:password@localhost:5432/smg"With a Connect Timeout¶
smg --history-backend postgres \
--postgres-db-url "postgres://user:password@db.example.com:5432/smg?connect_timeout=30"Redis Backend¶
High-performance caching with optional persistence and TTL-based retention.
Configuration Options¶
| Option | Default | Description |
|---|---|---|
--redis-url |
- | Redis connection URL |
--redis-pool-max-size |
16 |
Maximum connection pool size |
--redis-retention-days |
30 |
Data retention in days: at least 1, or -1 for persistent records. 0 is refused |
--redis-op-timeout-secs |
5 |
Bound in seconds on each store call (pool wait, connect, and the command; replaces the client library's 500 ms / 1 s defaults); a call past it is answered 503 storage_unavailable. Must be greater than 0 |
Connection URL Format¶
redis://[:password@]host[:port][/db]Examples¶
Basic Connection¶
smg --history-backend redis \
--redis-url "redis://localhost:6379"With a Password¶
smg --history-backend redis \
--redis-url "redis://:password@redis.example.com:6379"Persistent Storage¶
smg --history-backend redis \
--redis-url "redis://localhost:6379" \
--redis-retention-days=-1Oracle Backend¶
Enterprise-grade storage using Oracle Autonomous Database.
Configuration Options¶
| Option | Environment Variable | Default | Description |
|---|---|---|---|
--oracle-wallet-path |
ATP_WALLET_PATH |
- | Path to ATP wallet directory |
--oracle-tns-alias |
ATP_TNS_ALIAS |
- | TNS alias from tnsnames.ora |
--oracle-dsn |
ATP_DSN |
- | Direct connection descriptor |
--oracle-user |
ATP_USER |
- | Database username |
--oracle-password |
ATP_PASSWORD |
- | Database password |
--oracle-external-auth |
ATP_EXTERNAL_AUTH |
false |
Use external (OS) authentication instead of username/password; leave --oracle-user and --oracle-password unset |
--oracle-pool-min |
ATP_POOL_MIN |
1 |
Must be at least 1 and at most --oracle-pool-max. The pool opens connections on demand and doesn't hold a minimum open |
--oracle-pool-max |
ATP_POOL_MAX |
16 |
Maximum connection pool size |
--oracle-pool-timeout-secs |
ATP_POOL_TIMEOUT_SECS |
30 |
How long a request waits for a free pooled connection, in seconds. Must be greater than 0 |
--oracle-dsn takes precedence: with a DSN, SMG ignores the wallet and TNS alias. The Python launcher (smg launch from pip, and the container image) names two of these flags differently, --oracle-username and --oracle-connect-descriptor, and uses the TNS alias when both an alias and a DSN are given. See Python Launcher Differences.
Examples¶
Using ATP Wallet¶
smg --history-backend oracle \
--oracle-wallet-path /path/to/wallet \
--oracle-tns-alias mydb_high \
--oracle-user admin \
--oracle-password "$ORACLE_PASSWORD"Using Direct DSN¶
smg --history-backend oracle \
--oracle-dsn "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db.example.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice)))" \
--oracle-user admin \
--oracle-password "$ORACLE_PASSWORD"Schema Migrations¶
At startup, the PostgreSQL and Oracle backends create their tables if they don't exist, then compare the database's recorded schema version (the _schema_versions table) with the migrations SMG ships. On PostgreSQL this runs once per database, in one transaction under a schema lock, so gateways starting together take turns; a fresh, empty database gets its tables in their final shape and starts at the latest version, with nothing pending. On Oracle, a fresh database starts with every migration pending.
SMG applies pending migrations only when auto-migration is on. Otherwise it refuses to start and prints the SQL to apply by hand:
DB_AUTO_MIGRATE=true smg launch --history-backend postgres \
--postgres-db-url "postgres://user:password@localhost:5432/smg"DB_AUTO_MIGRATE=true (or 1) turns it on when no --schema-config file sets auto_migrate. In a schema config file, auto_migrate: true turns it on, and version: <n> marks migrations up to <n> as already applied. A database whose recorded version is ahead of the migrations the running build knows — usually after rolling the gateway image back — is refused at startup; set version: at or above the recorded version to run against it anyway. Redis and the in-memory backend have no migrations. See Schema Migrations.
What Gets Stored¶
Conversations¶
Container for a sequence of interactions:
- Conversation ID
- Creation timestamp
- Metadata (the key-value object the client attaches)
- Tenant identity of the creating request, when the gateway resolved one (see Tenant Scope)
Conversation Items¶
Individual items within a conversation:
| Type | Description |
|---|---|
| Messages | User and assistant messages with content |
| Reasoning | Model reasoning/thinking steps |
| Tool Calls | Tool invocations and results |
| MCP Calls | MCP server interactions |
| Function Calls | Function calling results |
Responses¶
Complete response records including:
- Input items
- Output (model response)
- Tool calls executed
- Model information
- Timestamps and metadata
- Token usage
- Tenant identity of the creating request, when the gateway resolved one (see Tenant Scope)
Tenant Scope¶
Every stored response, conversation, and conversation item records the tenant identity the gateway resolved for the request that created it, when it resolved one. A response or conversation stored under a tenant is served only to callers of that tenant.
That holds on every route that reads, continues, or deletes a stored record: a response's tenant gates getting it or its input items, deleting or cancelling it, and continuing it with previous_response_id; a conversation's tenant gates naming it as the conversation of a new response and every /v1/conversations operation. Another tenant's ID is answered exactly as an unknown ID — 404, or 400 (previous_response_not_found) for a previous_response_id — so a response or conversation ID tells a caller nothing about another tenant's records.
A conversation item's tenant guards it only where a request reaches the stored item directly by its ID, rather than through a conversation that holds it: an item_reference to another tenant's item answers 404 exactly as a missing item would, and adding an item under an id that another tenant's item already uses is refused with 400 (Item id '...' cannot be used) rather than serving or overwriting it.
Access to a conversation's items is governed by the conversation alone: every caller the conversation is visible to can list, get, and delete each of its items — and receives them as model context — whatever tenant an individual item records. Items a tenanted caller adds to a conversation stored without a tenant are therefore readable and deletable by every caller of that conversation.
A tenant is stored only for an identity the gateway can vouch for, as its canonical tenant key — the same key tenant rate limiting uses:
| Caller | Stored tenant |
|---|---|
A per-tenant API key (--tenant-api-key tenant_id:key) |
auth:<tenant_id> |
A trusted tenant header (--trust-tenant-header, header named by --tenant-header-name, default x-smg-tenant-id) |
header:<value> |
The shared --api-key |
auth: followed by the hex SHA-256 of the key — one identity for every caller of the key |
| No API key and no trusted header (callers known by IP address only, or anonymous) | None |
An identity from an API key wins over the header: a caller that authenticates with any API key is stored under that key's identity even when it also sends the trusted header.
The shared --api-key identity has two consequences: all of its callers share one scope and can read each other's stored history, and rotating the key changes the identity, so records stored under the old key answer as unknown IDs afterward.
A tenant ID can be at most 200 bytes: a longer --tenant-api-key tenant ID is refused at startup, and a longer trusted header value is rejected with 400 (invalid_tenant_header) rather than silently ignored.
Records stored without a tenant stay readable, continuable, and deletable by every caller. That covers records created while the gateway resolved no tenant identity for the caller (known by IP address only, or anonymous), and records stored before SMG recorded tenants — an upgrade does not retroactively scope existing records.
A history loaded through previous_response_id ends at the first record stored under another tenant, the same as it would at a missing record; when the referenced response itself belongs to another tenant, the request is rejected with 400 (previous_response_not_found). See the Responses API for the per-route errors.
All backends persist the tenant with each record; on PostgreSQL and Oracle the tenant column is added by schema migration 4 (see Schema Migrations).
Recommended Configurations¶
Production (Self-Hosted)¶
PostgreSQL for durable storage.
smg --history-backend postgres \
--postgres-db-url "postgres://smg:$DB_PASSWORD@postgres:5432/smg" \
--postgres-pool-max-size 32Enterprise (OCI)¶
Oracle for enterprise deployments.
smg --history-backend oracle \
--oracle-wallet-path /etc/smg/wallet \
--oracle-tns-alias smg_high \
--oracle-user smg_app \
--oracle-password "$ATP_PASSWORD" \
--oracle-pool-max 32Caching Layer¶
Redis for high-performance ephemeral storage.
smg --history-backend redis \
--redis-url "redis://:$REDIS_PASSWORD@redis.example.com:6379" \
--redis-retention-days 7 \
--redis-pool-max-size 64Troubleshooting¶
| Symptom | Cause | Solution |
|---|---|---|
Startup fails with history backend ... is unreachable at start-up |
SMG could not reach the store, or it didn't answer within the per-call timeout | Check the store and the connection URL |
Stateful endpoints answer 503 storage_unavailable |
The store is down, or slower than the per-call bound | Check the store; raise --postgres-op-timeout-secs / --redis-op-timeout-secs |
| Pool exhaustion | High concurrency | Increase pool size |
| Data not persisting | Wrong backend | Verify --history-backend setting |
| Redis data expiring | TTL too short | Increase --redis-retention-days |
Pool Configuration¶
# PostgreSQL
--postgres-db-url "postgres://...?connect_timeout=30"
--postgres-pool-max-size 64
# Oracle
--oracle-pool-timeout-secs 60
--oracle-pool-max 64
# Redis
--redis-pool-max-size 64