Registry / llm-agents / tokentrim

tokentrim

JSON →
library0.1.13pypypi✓ verified 84d ago

A utility library for trimming 'messages' arrays to fit within a specified token limit, commonly used with GPT models. Current version is 0.1.13, released as a stable package with no recent changes.

pip install tokentrim
INSTALL
IMPORT
SIG · TOKENTRIM
T
tokentrim
llm-agentspythonv0.1.13
Install
2.8s avg
Import
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 27.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.8s · import 0.000s · 28MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

trim
from tokentrim import trim
from tokentrim import trim_messages
format_function_calls
from tokentrim import format_function_calls
model_map
from tokentrim import model_map

Basic usage: trim a list of messages to a maximum token count for a given model.

import tokentrim messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me about Python."} ] # Trim messages to fit within 100 tokens trimmed_messages = tokentrim.trim_messages(messages, model="gpt-3.5-turbo", max_tokens=100) print(trimmed_messages)
Debug
Known issues
gotchaIf the last message is a user message and trimming removes it, the function may return an empty list or incomplete conversation.
fix
Ensure that the last message is preserved or handle the case where no messages remain.
affects: all
gotchaThe 'model' parameter must match a model supported by tiktoken; otherwise, it may fall back to a default encoding or raise an error.
fix
Use a known model string like 'gpt-3.5-turbo' or 'gpt-4'.
affects: all
deprecatedFuture versions may change default trimming behavior; check documentation for updates.
fix
Always specify max_tokens explicitly to avoid breaking changes.
affects: >=0.1.0
Upgrade
Version history
0.1.13latest on PyPI · released Nov 3, 2023
Audit
Dependencies
tiktokenrequiredTokenization for OpenAI models
Agent activity
36 hits · last 30 days
node
32
OpenAI (training)
1
Resources
tokentrim — pip install tokentrim · libregistry