Token Usage Tracking¶
The token usage tracking API provides detailed information about token consumption across different steps of the extraction process.
Overall token usage summary across all extraction steps.
Provides a complete view of token usage throughout the extraction process, including totals and per-step breakdowns.
Attributes:
Name | Type | Description |
---|---|---|
total_tokens |
int
|
Total tokens used across all steps |
prompt_tokens |
int
|
Total tokens used in prompts |
completion_tokens |
int
|
Total tokens generated in completions |
thinking_tokens |
Optional[int]
|
Total thinking/reasoning tokens (if available) |
cached_tokens |
Optional[int]
|
Total cached tokens (if available) |
steps |
List[Union[StepSummary, ExtractionSummary]]
|
List of per-step usage summaries |
Source code in structx/utils/usage.py
get_step(step_name)
¶
Get a step summary by its name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step_name
|
str
|
Name of the step to retrieve |
required |
Returns:
Type | Description |
---|---|
Optional[Union[StepSummary, ExtractionSummary]]
|
Step summary or None if not found |
Source code in structx/utils/usage.py
Token usage summary for a single step.
Provides a simple summary of token consumption for a specific step of the extraction process.
Attributes:
Name | Type | Description |
---|---|---|
tokens |
int
|
Number of tokens used in this step |
name |
str
|
Name of the step (analysis, refinement, etc.) |
Source code in structx/utils/usage.py
Token usage summary for extraction steps.
Extends StepSummary with additional details about individual extraction steps.
Attributes:
Name | Type | Description |
---|---|---|
tokens |
int
|
Number of tokens used across all extraction steps |
name |
str
|
Always "extraction" |
steps |
List[StepSummary]
|
Detailed breakdown of individual extraction calls |
Source code in structx/utils/usage.py
Get structured token usage information.
Provides a detailed breakdown of token usage across all steps of the extraction process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detailed
|
bool
|
If True, includes detailed breakdown of each extraction step (useful for multi-document extraction) |
False
|
Returns:
Type | Description |
---|---|
Optional[UsageSummary]
|
UsageSummary object with token usage information, or None if usage tracking |
Optional[UsageSummary]
|
isn't available |
Example
```python result = extractor.extract(data, query) usage = result.get_token_usage() print(f"Total tokens: {usage.total_tokens}")
Access step-specific usage¶
for step in usage.steps: print(f"{step.name}: {step.tokens} tokens")
Source code in structx/core/models.py
Aggregated token usage tracking across all extraction steps.
Stores and manages token usage data for the entire extraction process, providing methods to calculate totals and generate summaries.
Source code in structx/utils/usage.py
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
cached_tokens
property
¶
Total cached tokens
completion_tokens
property
¶
Total completion tokens
prompt_tokens
property
¶
Total prompt tokens
thinking_tokens
property
¶
Total thinking tokens
total_tokens
property
¶
Total tokens across all steps
add_step_usage(usage)
¶
Add usage information for a step
Source code in structx/utils/usage.py
get_usage_summary(detailed=False)
¶
Get structured token usage summary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detailed
|
bool
|
Whether to include detailed breakdown of extraction steps |
False
|
Returns:
Type | Description |
---|---|
UsageSummary
|
UsageSummary object with token usage information |