---
title: "Cap the size of every search · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/search/search-limits
updated: 2026-10-21
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Cap the size of every search

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Search](https://agentwranglers.se/playbook/data/search.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers). Updated 2026-10-21.

- ID: DAT-64
- Level: Recommended
- Chapter: [Search](https://agentwranglers.se/playbook/data/search)
- Rule: Reject search text over a fixed length, such as 200 characters, and return a fixed number of hits per page. Stop deep paging at a fixed depth, such as 1,000 hits.
- How: Long input makes many terms, and each term adds index reads. Ranking reads the `tsvector` of every match, so a common word on a large table is slow. Check the length on the server before the query runs, and answer 400 with the limit. Ask for one hit more than the page size to know whether a next page exists, instead of counting every match. Page size caps come from [Set a default page size and a maximum](https://agentwranglers.se/playbook/api-design/lists/page-size-limit). Time limits come from [Set time limits on every query](https://agentwranglers.se/playbook/data/operations/query-timeouts). Request limits for search come from [Limit requests per client](https://agentwranglers.se/playbook/adversarial-testing/abuse-and-limits/limit-requests).
- Check: Send a search of 201 characters. The answer is 400 and names the limit. Ask for page 51 at 20 hits a page. The answer is 400 or an empty page, within the query time limit.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
const MAX_QUERY_CHARACTERS = 200;
const MAX_SEARCH_DEPTH = 1000;
if (query.length > MAX_QUERY_CHARACTERS) return problem(400, `Search text is limited to ${MAX_QUERY_CHARACTERS} characters`);
if (offset + pageSize > MAX_SEARCH_DEPTH) return problem(400, `Search results stop at ${MAX_SEARCH_DEPTH} hits`);
```

### Python example

```python
MAX_QUERY_CHARACTERS = 200
MAX_SEARCH_DEPTH = 1000
if len(query) > MAX_QUERY_CHARACTERS:
    return problem(400, f"Search text is limited to {MAX_QUERY_CHARACTERS} characters")
if offset + page_size > MAX_SEARCH_DEPTH:
    return problem(400, f"Search results stop at {MAX_SEARCH_DEPTH} hits")
```

## Sources

- [PostgreSQL text search controls](https://www.postgresql.org/docs/current/textsearch-controls.html)

## Related rules

- [Cap the page size](https://agentwranglers.se/playbook/api-design/lists/page-size-limit) in API design
- [Set time limits on every query](https://agentwranglers.se/playbook/data/operations/query-timeouts) in Data
- [Limit requests per client](https://agentwranglers.se/playbook/adversarial-testing/abuse-and-limits/limit-requests) in Security testing

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/data/search/search-limits">Agent Wranglers data playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Set the language of every search field](https://agentwranglers.se/playbook/data/search/search-language)
- Next: [Test search results with a fixed query set](https://agentwranglers.se/playbook/data/search/search-quality-tests)
- [Search](https://agentwranglers.se/playbook/data/search)
