Resume a long conversation with a compacted history
Section: DOC-MA-conversations-context-management#manage-context-and-compaction.
Help a customer continue a long support thread without sending all older messages verbatim on every turn. This recipe summarizes older messages, preserves the ten most recent messages and checks the compaction record before the next reply.
Start with synchronous compaction when the next answer must wait for the summary. Use an existing thread with older messages beyond the preserved span, user-scoped credentials and permission for the summarizer call. Keep source records available when exact details matter: a summary can omit or distort them.
Choose the strategy deliberately. Once an explicit strategy is stored, the current API rejects switching to a different strategy. Use a suitable separate test conversation if the existing one is locked to another strategy.
Recipe: compact the older history, then continue the thread
Section: DOC-MA-conversations-context-management#get-started.
Replace support-chat-001 with the existing thread ID and keep that ID through all three steps:
-
Set a compaction threshold — configure the strategy and trigger on the thread. This example compacts synchronously at 80% of the model's context window, keeping the 10 most recent messages verbatim:
curl -X POST https://api.travila.ai/api/v1/llm/update-context-management-settings \
-H "X-API-Key: sk_your_key_here" \
-H "X-On-Behalf-Of: user_123" \
-H "Content-Type: application/json" \
-d '{
"conversationKey": "support-chat-001",
"contextManagementSettings": {
"strategy": "CONTEXT_STRATEGY_COMPACTION",
"compactionConfig": {
"mode": "COMPACTION_MODE_SYNC",
"threshold": {
"percentage": 80
},
"preserveRecent": 10
}
}
}'Reference: Update context management settings · Request fields.
-
Trigger a manual compaction — compact the thread now rather than waiting for the threshold. Useful at app launch before a user resumes a long conversation:
curl -X POST https://api.travila.ai/api/v1/llm/compact-conversation \
-H "X-API-Key: sk_your_key_here" \
-H "X-On-Behalf-Of: user_123" \
-H "Content-Type: application/json" \
-d '{
"conversationKey": "support-chat-001"
}'Reference: Compact a conversation · Request fields.
Response (sync — blocks until done):
{
"compactionId": "cmp_a1b2c3d4",
"tokensBefore": 48211,
"tokensAfter": 604,
"tokensSaved": 47607,
"durationMs": "4120",
"status": "COMPACTION_STATUS_COMPLETED"
}Reference: Compact a conversation · Response fields.
Treat the token counts as diagnostics. Confirm the compaction outcome and inspect the follow-up answer before considering this recipe complete.
-
Read the result — poll
conversation-stateand inspect thecompactionsarray:curl -X POST https://api.travila.ai/api/v1/llm/conversation-state \
-H "X-API-Key: sk_your_key_here" \
-H "X-On-Behalf-Of: user_123" \
-H "Content-Type: application/json" \
-d '{
"conversationKey": "support-chat-001"
}'Reference: Get full conversation state · Request fields.
Response (excerpt):
{
"compactionInProgress": false,
"compactions": [
{
"id": "cmp_a1b2c3d4",
"startSequence": "1",
"endSequence": "47",
"status": "COMPACTION_STATUS_COMPLETED",
"triggerReason": "COMPACTION_TRIGGER_REASON_MANUAL",
"originalTokenCount": 48211,
"summaryTokenCount": 604,
"modelUsed": "anthropic/claude-haiku-4"
}
]
}
Response excerpt: Get full conversation state · Response fields.
After the recorded compaction completes, send the customer's follow-up on the same thread and follow its generation outcome. Inspect whether the reply retains the details needed for the support task.
Finished result: the compaction record identifies the summarized message span, recent messages remain available verbatim, and the follow-up has a correlated outcome. The sample token counts illustrate a response; they are not a promised compression ratio, invoice saving or cache hit.
Verify the summary that the next reply can use
Section: DOC-MA-conversations-context-management#watch-a-compaction-complete.
Match the returned compactionId to its record in conversation-state.compactions. Check that it completed and that startSequence and endSequence cover the intended source messages. Retain the reported summarizer model and counts when investigating a changed answer; the state reference describes the record fields.
Variant: refresh the record from live updates (Preview)
Section: DOC-MA-conversations-context-management#realtime-events-preview.
When your deployment supports live compaction updates, use them to refresh the same compaction record.
Keep the state-read path for reconnects and deployments without that transport. The compaction ID and recorded result remain the basis for deciding whether to continue the recipe.
Recover when compaction fails before the reply
Section: DOC-MA-conversations-context-management#failure-behavior.
Inspect the failed compaction and the original reply before deciding whether to submit anything again. Recover the existing work using the linked failure behavior; do not resend the customer’s question simply to restart background summarization.
Variant: keep a window or full history instead
Section: DOC-MA-conversations-context-management#choose-a-strategy.
Choose windowing when older details are no longer needed. Select the strategy before saving the conversation setup and check the supported changes in the reference.
Keep full history with CONTEXT_STRATEGY_NONE only while the assembled request fits the selected provider’s limits. Use compaction when older context still matters and an additional summarizer call is acceptable, remembering that summaries can omit or distort details.
Tune the recipe for longer-running conversations
Section: DOC-MA-conversations-context-management#how-compaction-works.
Tune the recipe only after inspecting a completed compaction and its follow-up answer:
- Keep enough recent context.
preserveRecentcounts messages, not turns. Keep the recent exchange the customer will refer to; those messages follow the summary verbatim. - Leave headroom for the next request. Choose an absolute-token or percentage threshold below the provider’s actual limit. Prompts, tools and other request processing can change the final size.
- Choose the summarizer when needed. Supply a supported
generationConfiginsidecompactionConfigto change its model or sampling options; otherwise the platform uses its default compaction model. - Inspect the next answer and usage. Change one setting at a time. A completed summary is marked cache-preferred, but provider reuse and lower total cost are not guaranteed.
Use reported token estimates to choose context settings, not calculate an invoice. See usage and cost for measured and missing usage.
Variant: let the current turn proceed while a summary is pending
Section: DOC-MA-conversations-context-management#sync-vs-async.
Wait for the summary when the next answer needs it. Use background compaction only when the current reply can work with the existing history, then check that reply and the summary separately.
Resolve a compaction that stays pending
Section: DOC-MA-conversations-context-management#stale-pending-compactions.
Refresh the existing compaction record and inspect its current outcome before requesting another summary. A pending indicator alone does not prove work is still running.
Variant: omit a file or tool result that later replies no longer need
Section: DOC-MA-conversations-context-management#trim-content-selectively.
After an attachment or lookup has served its purpose, exclude it from later model inputs only if later questions do not need the original content. The example drops images and tool-result payloads; stored history is retained.
Example — strip images and tool results, keep everything else:
curl -X POST https://api.travila.ai/api/v1/llm/update-context-management-settings \
-H "X-API-Key: sk_your_key_here" \
-H "X-On-Behalf-Of: user_123" \
-H "Content-Type: application/json" \
-d '{
"conversationKey": "support-chat-001",
"contextManagementSettings": {
"strategy": "CONTEXT_STRATEGY_COMPACTION",
"selectiveExclusionConfig": {
"excludeToolResults": true,
"excludeImages": true
}
}
}'
Reference: Update context management settings · Request fields.
Exclude material only when later replies no longer need it. For example, removing a large image from later requests can reduce their size, but the model cannot re-inspect that image unless you provide it again.
Related
Section: DOC-MA-conversations-context-management#related.
- Configure how your agent responds — model, sampling, system prompt, and windowing config
- Send messages and get replies — the generation loop, polling, and sync mode
- Streaming availability — deployment-specific updates and state-read recovery
Document ID: DOC-MA-conversations-context-management. Section identities and revisions.