feat: expose token usage in CrawlResult (#1745)#1874
Open
hafezparast wants to merge 1 commit intounclecode:developfrom
Open
feat: expose token usage in CrawlResult (#1745)#1874hafezparast wants to merge 1 commit intounclecode:developfrom
hafezparast wants to merge 1 commit intounclecode:developfrom
Conversation
Add token_usage field to CrawlResult so LLM token consumption is included in the JSON response when using LLMExtractionStrategy. Changes: - models.py: add token_usage: Optional[Dict[str, Any]] to CrawlResult - async_webcrawler.py: after extraction, read total_usage from the extraction strategy and pass it to CrawlResult The token_usage dict contains prompt_tokens, completion_tokens, total_tokens, and optionally completion_tokens_details and prompt_tokens_details — matching the existing TokenUsage dataclass. Previously, token usage was only accessible via the strategy object (strategy.total_usage) in the Python SDK. Now it's serialized in CrawlResult, making it available via the Docker API endpoint too. Closes unclecode#1745 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
token_usagefield toCrawlResultso LLM token consumption is returned in the crawl response — including via the Docker API.What changed
crawl4ai/models.py:crawl4ai/async_webcrawler.py:After extraction completes, reads
config.extraction_strategy.total_usageand passes it toCrawlResult:Before / After
Before (Docker API response):
{ "extracted_content": "...", "success": true // no token usage }After:
{ "extracted_content": "...", "token_usage": { "prompt_tokens": 1234, "completion_tokens": 567, "total_tokens": 1801 }, "success": true }Python SDK (also works)
Test plan
Closes #1745
🤖 Generated with Claude Code