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

# From input to action

> Routing, context assembly, bounded tool use, and compaction inside one Entity turn.

Cognition turns an input into a response or action using the Entity's identity, state, retrieved memories, and available tools. Reflex and deliberate are routing profiles; their model IDs can be configured independently.

## Routing

The router begins with heuristics about the input and current state. It can use a small model classification when the heuristic is uncertain. Configuration and special runtime inputs can force deliberate processing.

| Profile    | Purpose                              | Budget source                                                |
| ---------- | ------------------------------------ | ------------------------------------------------------------ |
| Reflex     | Short or straightforward interaction | Harness `cognition.reflex_max_tokens`                        |
| Deliberate | More involved reasoning and work     | Entity override or harness `cognition.deliberate_max_tokens` |

The checked-in harness defaults are 1,024 reflex output tokens and 16,384 deliberate output tokens. Provider behavior, model configuration, and the active route determine what is actually used.

## Turn assembly

The runtime combines stable identity instructions with changing context: body state, current input, relevant knowledge and episodes, relationship context, procedures, projects, and tool availability.

Not every stored object belongs in every prompt. Retrieval and context budgets choose a working subset, while the authoritative state remains outside the model request.

## Agent loop

The tool loop submits the current context, receives tool requests, executes registered calls, and returns their results to the model. Native tool mode includes agency tools for speaking and ending a turn, alongside work tools for files, research, execution, and spatial actions.

The loop has bounded steps and continuation rounds. It tracks repeated work and completion signals so a tool-rich turn can make progress without continuing indefinitely. Explicit turn completion is treated as a completion signal; control-plane cancellation does not ask the model to decide whether to stop.

Multiple tool calls can be dispatched within a round. Treat external tool effects as real side effects: cancelling later inference does not roll back a file write, message, or other completed action.

## Context compaction

Automatic compaction runs before inference when estimated input size crosses the configured fraction of the effective working budget. The default threshold ratio is `0.6`.

<Steps>
  <Step title="Protect continuity">
    Preserve the first exchange and a recent tail. Choose boundaries that keep tool-call and result pairs coherent.
  </Step>

  <Step title="Retain useful facts">
    When enabled, extract durable facts from older context into knowledge. This is best-effort and does not make summarization lossless.
  </Step>

  <Step title="Summarize the middle">
    Reduce older tool output and summarize the middle conversation into a structured handoff that includes the existing rolling summary.
  </Step>

  <Step title="Commit a successful summary">
    Replace the compacted middle only after a usable summary is available. Report progress to the connected surface.
  </Step>
</Steps>

```yaml theme={null}
cognition:
  max_context_tokens: 16384
  history_compression:
    enabled: true
    compaction_threshold_ratio: 0.6
    compaction_target_ratio: 0.08
    compaction_protect_first_n: 2
    compaction_protect_last_n: 12
    compaction_max_passes: 3
    compaction_flush_to_knowledge: true
```

These values configure the application. The native OpenAI Astra route currently uses a separate 256,000-token effective working budget without resizing an Ollama context cache.

Token estimates are approximate. Compaction reduces the risk of exceeding a request budget; it cannot guarantee that every provider, tool schema, or unusual input fits.

## Manual controls

Telegram `/compact` and the spatial context control request summarization. `/reset` clears the live conversation instead. Neither deletes durable memory. [Usage and controls](/guides/usage-and-controls) explains pause, cancellation, and compaction feedback.

Source: [`entity.py`](https://github.com/ngramspatial/ngram/blob/main/ngram/entity.py), [`cognition/deliberate.py`](https://github.com/ngramspatial/ngram/blob/main/ngram/cognition/deliberate.py), and [`cognition/history_compression.py`](https://github.com/ngramspatial/ngram/blob/main/ngram/cognition/history_compression.py).
