> ## Documentation Index
> Fetch the complete documentation index at: https://docs.maximem.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# conversation.context.compact

> Trigger asynchronous compaction of a conversation into a compressed summary.

```python theme={null}
await sdk.conversation.context.compact(
    conversation_id: str,
    strategy: Optional[str] = None,
    compaction_level: Optional[str] = None,
    target_tokens: Optional[int] = None,
    force: bool = False,
) -> CompactionTriggerResponse
```

Kicks off compaction for a conversation. Compaction runs asynchronously. This call returns a handle with a `compaction_id` and an `in_progress` status. Use [`get_compaction_status`](/sdk-reference/conversation-context/get-compaction-status) to poll for completion, then [`get_compacted`](/sdk-reference/conversation-context/get-compacted) to retrieve the resulting summary.

If a previous compacted summary already exists, it is returned on the trigger response as `previous_context` so you have something usable while the new compaction runs.

### Parameters

<ParamField path="conversation_id" type="str" required={true}>
  The conversation to compact. Must be a valid UUID [registered via `record_message`](/concepts/context-end-to-end#short-term-context).
</ParamField>

<ParamField path="strategy" type="str" required={false}>
  Override the compaction strategy. One of:

  * `"aggressive"`: maximum compression, shortest output
  * `"balanced"`: middle-ground compression
  * `"conservative"`: preserve more detail
  * `"adaptive"`: let Synap pick based on conversation shape
</ParamField>

<ParamField path="compaction_level" type="str" required={false}>
  Backward-compatible alias for `strategy`. Prefer `strategy` in new code.
</ParamField>

<ParamField path="target_tokens" type="int" required={false}>
  Override the target token budget for the compacted output.
</ParamField>

<ParamField path="force" type="bool" required={false} default="False">
  Compact even if the conversation has not crossed the automatic threshold.
</ParamField>

### Returns

A `CompactionTriggerResponse` describing the in-flight job.

<ResponseField name="compaction_id" type="str">Identifier for this compaction run.</ResponseField>
<ResponseField name="conversation_id" type="str">Echo of the supplied conversation id.</ResponseField>
<ResponseField name="status" type="str">Initial status, typically `"in_progress"`.</ResponseField>
<ResponseField name="trigger_type" type="str">How compaction was triggered (e.g., `"manual_api"`).</ResponseField>
<ResponseField name="initiated_at" type="datetime">When the run started.</ResponseField>
<ResponseField name="estimated_completion_seconds" type="int">Rough ETA in seconds.</ResponseField>
<ResponseField name="previous_context" type="Dict[str, Any] | None">Previously compacted context, if any. Usable while the new run finishes.</ResponseField>
<ResponseField name="previous_context_age_seconds" type="int | None">Age of `previous_context` in seconds.</ResponseField>
<ResponseField name="previous_compaction_id" type="str | None">Identifier of the previous compaction run.</ResponseField>

### Example

```python theme={null}
import uuid
from maximem_synap import MaximemSynapSDK

sdk = MaximemSynapSDK(api_key="synap_your_key_here")
await sdk.initialize()

# conversation_id must be a valid UUID; reuse the conversation's id
conversation_id = str(uuid.uuid4())

job = await sdk.conversation.context.compact(
    conversation_id=conversation_id,
    strategy="balanced",
    target_tokens=800,
)

print(f"Compaction {job.compaction_id} started (status: {job.status})")
if job.previous_context:
    print("Previous compacted context still usable while new run completes.")
```

### Raises

* `AuthenticationError`: when the API key is missing or invalid.
* `NetworkError`: when the SDK cannot reach Synap.

### See also

* [conversation.context.get\_compaction\_status](/sdk-reference/conversation-context/get-compaction-status)
* [conversation.context.get\_compacted](/sdk-reference/conversation-context/get-compacted)
* [conversation.context.get\_context\_for\_prompt](/sdk-reference/conversation-context/get-context-for-prompt)
