ccproxy.llms.utils¶
ccproxy.llms.utils
¶
LLM utility modules for token estimation and context management.
truncate_to_fit
¶
Truncate request to fit within token limit.
Strategy: 1. Always preserve system prompt and tools 2. Try to preserve the last N messages (preserve_recent) 3. Remove oldest messages first 4. If too few messages to truncate, reduce preserve_recent dynamically 5. As a last resort, truncate large content blocks within messages 6. Add a truncation notice when content is removed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_data
|
dict[str, Any]
|
The request payload |
required |
max_input_tokens
|
int
|
Model's max input token limit |
required |
preserve_recent
|
int
|
Number of recent messages to always keep |
10
|
safety_margin
|
float
|
Target this fraction of max to allow for estimation error |
0.9
|
Returns:
| Type | Description |
|---|---|
tuple[dict[str, Any], bool]
|
Tuple of (modified_request_data, was_truncated) |
Source code in ccproxy/llms/utils/context_truncation.py
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
estimate_messages_tokens
¶
Estimate total tokens for a list of messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[dict[str, Any]]
|
List of message dicts with role and content |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated total token count |
Source code in ccproxy/llms/utils/token_estimation.py
estimate_request_tokens
¶
Estimate total input tokens for a request.
Includes messages, system prompt, and tool definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_data
|
dict[str, Any]
|
The request payload dictionary |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated total input token count |
Source code in ccproxy/llms/utils/token_estimation.py
estimate_tokens
¶
Estimate token count for content.
Uses ~3 characters per token heuristic for English text. This is a conservative estimate - actual may be lower.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
Message content (string, list of blocks, or dict) |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated token count |
Source code in ccproxy/llms/utils/token_estimation.py
get_max_input_tokens
¶
Get max input tokens for a model.
Supports pattern matching for model variants: - Exact match: "claude-opus-4-5-20251101" - Prefix match: "claude-opus-4-5-*" matches "claude-opus-4-5-20251101"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model name or identifier |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
Max input tokens if known, None otherwise |