pyagent-compress API Reference¶
pyagent_compress.compressor.MessageCompressor
¶
Compress inter-agent messages by removing filler and extracting key sentences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_ratio
|
float
|
Target compression ratio (0.3 = keep 30% of tokens). |
0.5
|
min_sentence_length
|
int
|
Minimum characters for a sentence to be kept. |
20
|
remove_filler
|
bool
|
Whether to strip filler phrases. |
True
|
Source code in packages/pyagent-compress/src/pyagent_compress/compressor.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
compress(text)
¶
Compress a message text.
Strategy: 1. Remove filler phrases 2. Split into sentences 3. Score sentences by information density 4. Keep top sentences until target ratio met
Source code in packages/pyagent-compress/src/pyagent_compress/compressor.py
pyagent_compress.compressor.CompressionResult
dataclass
¶
Result of message compression.
Attributes:
| Name | Type | Description |
|---|---|---|
original |
str
|
The original text. |
compressed |
str
|
The compressed text. |
original_tokens |
int
|
Estimated original token count. |
compressed_tokens |
int
|
Estimated compressed token count. |
savings_pct |
float
|
Percentage of tokens saved (0-1). |
Source code in packages/pyagent-compress/src/pyagent_compress/compressor.py
pyagent_compress.pruner.AgentPruner
¶
Detect and flag non-contributing agents for removal.
Scores each agent's contribution based on: - Unique information (not repeated from other agents) - Response diversity (different from own previous responses) - Task relevance (overlap with original task keywords)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_contribution
|
float
|
Minimum contribution score (0-1) to keep an agent. |
0.3
|
window_size
|
int
|
Number of recent messages to analyze per agent. |
5
|
Source code in packages/pyagent-compress/src/pyagent_compress/pruner.py
score_agents(messages, task)
¶
Score each agent's contribution from message history.
Source code in packages/pyagent-compress/src/pyagent_compress/pruner.py
should_prune(scores)
¶
Return agent names that should be pruned (below min contribution).
pyagent_compress.pruner.InteractionPruner
¶
Detect early consensus and skip remaining communication rounds.
Analyzes agent outputs to determine if consensus has been reached, allowing patterns like Debate or Voting to terminate early.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consensus_threshold
|
float
|
Minimum similarity between agent outputs to consider consensus reached (0-1). |
0.7
|
min_rounds
|
int
|
Minimum rounds before early termination is allowed. |
1
|
Source code in packages/pyagent-compress/src/pyagent_compress/pruner.py
has_consensus(outputs, current_round)
¶
Check if agents have reached consensus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outputs
|
list[str]
|
Current round's outputs from all agents. |
required |
current_round
|
int
|
The current round number (1-indexed). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if consensus is reached and we can stop early. |
Source code in packages/pyagent-compress/src/pyagent_compress/pruner.py
pyagent_compress.pruner.ContributionScore
dataclass
¶
How much an agent is contributing to the conversation.
Source code in packages/pyagent-compress/src/pyagent_compress/pruner.py
pyagent_compress.budget.TokenBudget
dataclass
¶
Manage token budgets across a multi-agent workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workflow_limit
|
int
|
Total token limit across all agents. |
100000
|
per_agent_limit
|
int
|
Default per-agent token limit. |
20000
|
strict
|
bool
|
If True, raises BudgetExceededError on limit violations. |
True
|
Source code in packages/pyagent-compress/src/pyagent_compress/budget.py
consume(agent_name, tokens)
¶
Record token consumption for an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_name
|
str
|
The consuming agent's name. |
required |
tokens
|
int
|
Number of tokens consumed. |
required |
Source code in packages/pyagent-compress/src/pyagent_compress/budget.py
register_agent(name, limit=None)
¶
Register an agent with an optional custom limit.
remaining(agent_name=None)
¶
Get remaining tokens for an agent or the whole workflow.
Source code in packages/pyagent-compress/src/pyagent_compress/budget.py
summary()
¶
Return a summary of all agent budgets.
Source code in packages/pyagent-compress/src/pyagent_compress/budget.py
pyagent_compress.budget.AgentBudget
dataclass
¶
Budget tracking for a single agent.
Source code in packages/pyagent-compress/src/pyagent_compress/budget.py
consume(tokens, strict=True)
¶
Consume tokens from budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
int
|
Number of tokens consumed. |
required |
strict
|
bool
|
If True, raises BudgetExceededError. If False, just tracks. |
True
|
Source code in packages/pyagent-compress/src/pyagent_compress/budget.py
pyagent_compress.budget.BudgetExceededError
¶
Bases: Exception
Raised when a token budget is exceeded.
Source code in packages/pyagent-compress/src/pyagent_compress/budget.py
pyagent_compress.middleware.CompressMiddleware
¶
Middleware that wraps agents with automatic output compression.
Usage
middleware = CompressMiddleware(target_ratio=0.5) compressed_agent = middleware.wrap(my_agent)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compressor
|
MessageCompressor | None
|
Optional MessageCompressor. Created with defaults if None. |
None
|
budget
|
TokenBudget | None
|
Optional TokenBudget for workflow-wide tracking. |
None
|
target_ratio
|
float
|
Target compression ratio (used if compressor not provided). |
0.5
|
Source code in packages/pyagent-compress/src/pyagent_compress/middleware.py
wrap(agent)
¶
Wrap an agent with compression.
pyagent_compress.middleware.CompressedAgent
¶
Bases: Agent
Agent wrapper that compresses output messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
The original agent. |
required |
compressor
|
MessageCompressor
|
MessageCompressor instance. |
required |
budget
|
TokenBudget | None
|
Optional TokenBudget for tracking. |
None
|
Source code in packages/pyagent-compress/src/pyagent_compress/middleware.py
run(messages)
async
¶
Run the agent and compress the output.