# Auto-Batching

Your plan caps how many Terms a single request may carry: the **Max Terms per Operation** limit, 5 on the Free plan (see [Plans and Pricing](/plans-pricing)). Auto-batching lets you call `union`, `intersection` and `concat` with more Terms than that and still get a single result back.

It is **enabled by default** and requires no configuration.

## How it works

The first time a client needs them, it fetches your account limits from the API and caches them for its lifetime. When a call carries more Terms than `maxTermsCount`, the client:

1. Splits the Term list into chunks that fit within the cap.
2. Sends one request per chunk.
3. Feeds the results back into further requests until a single Term remains.

This works because `union`, `intersection` and `concat` are associative: combining the results of the chunks gives the same language as combining every Term at once. Operations that take a fixed number of Terms, such as `difference`, `equivalent` and `subset`, are never batched.

<CodeTabs defaultValue="java">
<CodeTabs.Tab value="java" label="Java">
```java
RegexSolverClient client = RegexSolverClient.builder()
    .apiToken(System.getenv("REGEXSOLVER_API_TOKEN"))
    .build();

// 12 terms on a plan that allows 5 per request
List<Term> terms = new ArrayList<>();
for (String pattern : patterns) {
    terms.add(Term.regex(pattern));
}

// A single call; the client issues several requests
Term result = client.union(terms);
```
</CodeTabs.Tab>

<CodeTabs.Tab value="javascript" label="JavaScript">
```javascript
const client = new RegexSolverClient({ apiToken: process.env.REGEXSOLVER_API_TOKEN });

// 12 terms on a plan that allows 5 per request
const terms = patterns.map((p) => Term.regex(p));

// A single call; the client issues several requests
const result = await client.union(...terms);
```
</CodeTabs.Tab>

<CodeTabs.Tab value="python" label="Python">
```python
client = RegexSolverClient(api_token=os.getenv("REGEXSOLVER_API_TOKEN"))

# 12 terms on a plan that allows 5 per request
terms = [Term.regex(p) for p in patterns]

# A single call; the client issues several requests
result = client.union(*terms)
```
</CodeTabs.Tab>
</CodeTabs>

## What it costs

Each request the client sends counts against your monthly quota and your rate limit, and the chunks are separate round trips. A `union` of 12 Terms on the Free plan therefore consumes several requests and takes correspondingly longer.

## Disabling it

Turn auto-batching off at client construction if you would rather oversized calls failed explicitly, so that you control how they are split. Calls above the cap then raise `TooManyTerms` (see [Error Handling](/error-handling)).

<CodeTabs defaultValue="java">
<CodeTabs.Tab value="java" label="Java">
```java
RegexSolverClient client = RegexSolverClient.builder()
    .apiToken(System.getenv("REGEXSOLVER_API_TOKEN"))
    .autoBatch(false)
    .build();

try {
    Term result = client.union(manyTerms); // more terms than the plan allows
} catch (TooManyTermsException e) {
    System.out.println("Split the call yourself: " + e.getMessage());
}
```
</CodeTabs.Tab>

<CodeTabs.Tab value="javascript" label="JavaScript">
```javascript
const client = new RegexSolverClient({
    apiToken: process.env.REGEXSOLVER_API_TOKEN,
    autoBatch: false,
});

try {
    const result = await client.union(...manyTerms); // more terms than the plan allows
} catch (e) {
    if (e instanceof TooManyTermsError) {
        console.log(`Split the call yourself: ${e.message}`);
    }
}
```
</CodeTabs.Tab>

<CodeTabs.Tab value="python" label="Python">
```python
client = RegexSolverClient(
    api_token=os.getenv("REGEXSOLVER_API_TOKEN"),
    auto_batch=False,
)

try:
    result = client.union(*many_terms)  # more terms than the plan allows
except TooManyTermsError as e:
    print(f"Split the call yourself: {e}")
```
</CodeTabs.Tab>
</CodeTabs>

<Callout type="note">
Auto-batching splits by Term count only. It does not split work that is heavy because a single Term is complex. For that, see [Bounding Execution](/advance-usage/bounding-execution) and [Heavy Operations](/advance-usage/heavy-operations).
</Callout>
