Finding the costs of extractig a page #1646
Unanswered
JonasPapinigis
asked this question in
Forums - Q&A
Replies: 1 comment 2 replies
-
|
We just addressed this — PR #1874 adds a To try it now before it's merged: pip install git+https://github.com/hafezparast/crawl4ai.git@fix/token-usage-in-crawl-result-1745Then: result = await crawler.arun(url, config=config)
print(result.token_usage)
# {'prompt_tokens': 1234, 'completion_tokens': 567, 'total_tokens': 1801}If you prefer to stay on the current release, token usage is already tracked on the strategy object — it's just not exposed in strategy = LLMExtractionStrategy(
llm_config=LLMConfig(provider="openai/gpt-4o", api_token="..."),
instruction="...",
)
config = CrawlerRunConfig(extraction_strategy=strategy)
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(url, config=config)
# Token usage is on the strategy object
print(strategy.total_usage) # Accumulated across all chunks
print(strategy.usages) # Per-chunk breakdown
strategy.show_usage() # Pretty-printed summaryFor cost calculation, multiply |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm looking to use this library on a relatively large scale seeded extraction. I wanted to know there is a way to expose the amount of tokens used in a single-(or even multi-)page extraction/crawl? I know I get the CrawlResult for each page, but I don't see any input/output token consumption metrics anywhere (possibly in metadata?).
My inference provider does not have a way to view EXACT costs, but I want to be able to make something like this for myself: https://github.com/orkunkinay/openai_cost_calculator
Thanks for any help!
Beta Was this translation helpful? Give feedback.
All reactions