ContextWindow.fit_messages()
Fit a list of conversation messages into the token budget.
Usage
ContextWindow.fit_messages(
messages,
*,
system_prompt="",
strategy=None,
)The system prompt is always preserved. Messages are dropped according to the selected strategy until they fit within the remaining budget.
Parameters
messages: list[dict[str, str] | Message]-
List of message dicts (with
"role"and"content"keys) or~talk_box.conversation.Messageobjects. system_prompt: str = ""-
The system prompt text (always preserved in full).
strategy: str | FitStrategy | None = None- Override the default strategy for this call.
Returns
FitResult- The fitted messages plus metadata about what was dropped.
Examples
import talk_box as tb
ctx = tb.ContextWindow(max_tokens=4096, reserve_output=512)
messages = [
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi there! How can I help?"},
{"role": "user", "content": "Tell me about Python."},
]
result = ctx.fit_messages(messages, system_prompt="You are helpful.")
print(f"Using {result.tokens_used}/{result.token_budget} tokens")
print(f"Dropped {result.messages_dropped} messages")