Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crawl4ai/async_webcrawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ async def aprocess_html(
################################
# Structured Content Extraction #
################################
token_usage = None
if (
not bool(extracted_content)
and config.extraction_strategy
Expand Down Expand Up @@ -914,6 +915,17 @@ async def aprocess_html(
extracted_content, indent=4, default=str, ensure_ascii=False
)

# Capture token usage from extraction strategy
if hasattr(config.extraction_strategy, 'total_usage'):
_token_usage = config.extraction_strategy.total_usage
if _token_usage and hasattr(_token_usage, '__dict__'):
token_usage = {
k: v for k, v in _token_usage.__dict__.items()
if v is not None and v != 0
} or None
else:
token_usage = None

# Log extraction completion
self.logger.url_status(
url=_url,
Expand All @@ -940,6 +952,7 @@ async def aprocess_html(
screenshot=screenshot_data,
pdf=pdf_data,
extracted_content=extracted_content,
token_usage=token_usage,
success=True,
error_message="",
)
Expand Down
2 changes: 2 additions & 0 deletions crawl4ai/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class CrawlResult(BaseModel):
cache_status: Optional[str] = None # "hit", "hit_validated", "hit_fallback", "miss"
# Anti-bot retry/proxy usage stats
crawl_stats: Optional[Dict[str, Any]] = None
# LLM token usage (populated when using LLMExtractionStrategy)
token_usage: Optional[Dict[str, Any]] = None

model_config = ConfigDict(arbitrary_types_allowed=True)

Expand Down