> For the complete documentation index, see [llms.txt](https://docs.meganova.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.meganova.ai/character-studio/advanced/token-management.md).

# Token Management

> **Note:** Understanding tokens and context windows is essential for optimizing character performance. Learn how to manage context efficiently.

## Overview

Token management involves:

* **Understanding** what tokens are
* **Managing** context window usage
* **Optimizing** character definitions
* **Balancing** detail vs. space

## What Are Tokens?

### Definition

Tokens are the units AI models use to process text:

* \~4 characters per token (English)
* Words may be 1-3 tokens
* Punctuation and spaces are tokens

### Examples

| Text            | Tokens |
| --------------- | ------ |
| "Hello"         | 1      |
| "Hello, world!" | 4      |
| "Extraordinary" | 4      |
| "AI"            | 1      |

### Checking Token Count

Character Studio shows token counts:

* In Blueprint Editor preview
* Per section breakdown
* Total character tokens

## Context Window

### What It Is

The context window is the AI's "working memory":

* Total tokens it can consider
* Includes everything sent to AI
* Limited by model

### What Goes In Context

```
[System Prompt]
+ [Character Definition]
+ [Lorebook Entries (triggered)]
+ [Chat History]
+ [Current Message]
= Total Context
```

### Model Context Sizes

| Model         | Context Window |
| ------------- | -------------- |
| GPT-4o-mini   | 128K tokens    |
| GPT-4o        | 128K tokens    |
| Claude Sonnet | 200K tokens    |
| Llama 3.1-8B  | 128K tokens    |

## Context Budget

### Allocation Strategy

Typical budget allocation:

| Component          | % of Context |
| ------------------ | ------------ |
| System + Character | 10-15%       |
| Lorebook budget    | 10-25%       |
| Chat history       | 50-70%       |
| Current exchange   | 5-10%        |

### Example (8K Context)

```
Total: 8,000 tokens

System/Character: 1,000 (12.5%)
Lorebook: 1,500 (18.75%)
Chat history: 5,000 (62.5%)
Current: 500 (6.25%)
```

## Optimizing Character Definitions

### Efficient Writing

**Verbose (150 tokens):**

```
Elena is a very old and wise elven wizard who has been alive for
many hundreds of years. During this time, she has learned an
incredible amount about magic and has become one of the most
knowledgeable wizards in all the land. She runs a library that
contains many ancient and valuable magical texts.
```

**Efficient (60 tokens):**

```
Elena is a centuries-old elven wizard, among the most knowledgeable
in magical arts. She runs the Library of Shadows, housing rare
magical texts.
```

### Priority Order

Put essential info first (in case of truncation):

1. Name and core identity
2. Key personality traits
3. Speech patterns
4. Important behaviors
5. Background details (brief)
6. Nice-to-have details

### What to Move to Lorebook

Move from character definition to lorebook:

* Detailed world information
* Extended backstory
* Supporting character details
* Historical events
* Specific lore

## Lorebook Token Budget

### Setting Budget

Configure in Character Studio:

* 10%: Minimal lore use
* 25%: Balanced (default)
* 50%: Heavy world-building

### Efficient Entries

**Verbose entry:**

```
The Library of Shadows is an ancient and mysterious building that
was constructed many thousands of years ago by powerful wizards
who wanted to preserve magical knowledge...
```

**Efficient entry:**

```
Library of Shadows: Ancient repository of magical texts, built
millennia ago by the Arcane Council. Hidden in the Whispering
Mountains. Contains both preserved knowledge and forbidden tomes.
```

## Chat History Management

### Automatic Trimming

As conversation grows:

1. Oldest messages trimmed first
2. System prompt always kept
3. Recent messages prioritized

### Managing Long Conversations

Strategies:

* **Summarize**: Recap key points periodically
* **Pin memories**: Critical facts stay in context
* **New sessions**: Start fresh when context cluttered

## Monitoring Usage

### In Character Studio

View token usage:

* Character definition tokens
* Lorebook tokens
* Session context usage

### Token Counter

Blueprint Editor shows:

```
Character: 850 tokens
├── Identity: 150
├── Background: 300
├── Behavior: 250
└── Dialogue: 150
```

## Troubleshooting

### Character "forgets" things

**Causes:**

* Old messages trimmed
* Info not prominent enough
* Context too full

**Solutions:**

* Pin important memories
* Put critical info in character definition
* Summarize key points

### Responses cut off

**Causes:**

* Max tokens too low
* Context nearly full

**Solutions:**

* Increase max tokens setting
* Trim character definition
* Start new session

### Lorebook not triggering

**Causes:**

* Budget exhausted
* Keyword not matched
* Priority too low

**Solutions:**

* Increase lorebook budget
* Check keywords
* Raise entry priority

## Best Practices

### 1. Measure First

Know your token usage before optimizing.

### 2. Prioritize

Keep essential info, trim nice-to-have.

### 3. Use Lorebook Wisely

Keyword-triggered content is efficient.

### 4. Monitor Sessions

Watch for context-full symptoms.

### 5. Test with Limits

Verify character works at intended context size.

## See Also

* [Advanced Settings](https://docs.meganova.ai/character-studio/character-features/advanced-settings.md) — Model selection
* [Lorebooks](https://docs.meganova.ai/character-studio/character-features/lorebooks.md) — Lorebook management
* [Memory System](https://docs.meganova.ai/character-studio/chat-features/memory-system.md) — Pinned memories
* [Character Specs](https://docs.meganova.ai/character-studio/advanced/character-specs.md) — Data formats
