# POST /agent-chat-compact

**Resource:** [Evie (In-App Agent)](./evie.md)  
**Scopes:** _none_  
**Write operation:** yes

Compact a long Evie conversation thread into a new thread seeded with a summary of the original. Call this when a thread grows too long for efficient context handling. The original thread is marked as compacted (superseded_by_thread_id is set); the returned new_thread_id is ready for continued conversation. A mechanical compaction pass runs first (free); if the thread is still too long after that, a lightweight AI summarisation pass runs (small credit cost). The new thread starts with one assistant message summarising the conversation so far.

## Parameters

| Name | In | Type | Required | Description |
|------|----|------|----------|-------------|
| `thread_id` | body | uuid | yes | The thread to compact. Must belong to the authenticated (user, company) pair. |

## Request example

```bash
POST /functions/v1/agent-chat-compact
Authorization: Bearer tp_oauth_a1b2c3d4e5f6...
Content-Type: application/json

{
  "thread_id": "a1b2c3d4-..."
}
```

## Response example

```json
{
  "new_thread_id": "e5f6a7b8-...",
  "parent_thread_id": "a1b2c3d4-...",
  "summary_source": "mechanical",
  "stats": {
    "original_messages": 28,
    "after_messages": 6,
    "before_tokens": 9200,
    "after_tokens": 3100,
    "llm_input_tokens": 0,
    "llm_output_tokens": 0
  }
}
```

## Notes

- summary_source is "mechanical" (deterministic, zero LLM cost) or "haiku" (AI summary, small credit cost).
- The original thread cannot be compacted twice (returns 409 if already superseded).
- Continue the conversation by POSTing to /agent-chat with the new_thread_id.

---
Base URL: `https://api.trustpager.com/functions/v1/api/v1` — Auth: `Authorization: Bearer YOUR_API_KEY`