Skip to content

[Bug]: BestFirstCrawlingStrategy fetches a full batch past max_pages #2381

Open

Description

@loickouao

crawl4ai version

0.9.5 (same on 0.9.0)

Expected Behavior

BestFirstCrawlingStrategy with max_pages=N fetches about N pages: the last batch is cut to the remaining budget.

Current Behavior

_arun_best_first computes batch_size = min(BATCH_SIZE, remaining) but pops range(BATCH_SIZE) items, so the last batch is always 10 URLs. They are all fetched, and the results past the limit are dropped. With max_pages=12, 21 pages are fetched for 12 results (24 on 0.9.0).

Is this reproducible?

Yes

Code snippets

import asyncio

from aiohttp import web
from crawl4ai import AsyncWebCrawler, CrawlerRunConfig
from crawl4ai.deep_crawling import BestFirstCrawlingStrategy

hits = []


async def handle(request):
    hits.append(request.path)
    links = "".join(f'<a href="/p{i}">p{i}</a>' for i in range(30)) if request.path == "/" else ""
    return web.Response(text=f"<html><body>{links}<p>{'text ' * 200}</p></body></html>", content_type="text/html")


async def main():
    app = web.Application()
    app.router.add_get("/{tail:.*}", handle)
    runner = web.AppRunner(app)
    await runner.setup()
    await web.TCPSite(runner, "127.0.0.1", 8766).start()
    config = CrawlerRunConfig(deep_crawl_strategy=BestFirstCrawlingStrategy(max_depth=1, max_pages=12), stream=True)
    async with AsyncWebCrawler() as crawler:
        results = [r async for r in await crawler.arun("http://127.0.0.1:8766/", config=config)]
    print("results:", len(results), "pages fetched:", len([h for h in hits if h != "/robots.txt"]))  # 12 vs 21
    await runner.cleanup()


asyncio.run(main())

Suggested fix

for _ in range(batch_size): in the batch-filling loop. The open PR #2246 (configurable batch_size) keeps the same pattern: it computes effective_batch_size but loops over range(self.batch_size).

Activity

  1. added a commit that references this issue on Oct 15, 2026
    98a2e04
  2. added a commit that references this issue on Oct 15, 2026
    e11d132
  3. yashikam19 commented on Oct 15, 2026

    @yashikam19

    Thanks @loickouao, good catch, and you're right that #2246 had the same problem.

    I have pushed a fix to #2246: the batch loop now iterates over effective_batch_size, so the final round is capped by the remaining max_pages budget.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞 BugSomething isn't working🩺 Needs TriageNeeds attention of maintainers

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions