Why token size matters
A JWT travels with every request: in an Authorization header or in a cookie. Each claim you add makes every request a little larger, and past certain sizes requests start to fail. Browsers drop cookies that are too large, and servers and proxies reject requests whose headers exceed their limits.
The failures are confusing because they depend on the user. A user with three roles works; a user with three hundred groups gets an error page. Measuring the token tells you how close you are to a limit and which claims to trim.
How a token’s size adds up
A signed JWT is three Base64url parts joined by dots. Base64url writes every 3 bytes as 4 characters, so the header and payload grow by about a third compared with their JSON. The signature has a fixed size for each algorithm.
| Algorithm | Signature bytes | Base64url characters |
|---|---|---|
| HS256 | 32 | 43 |
| ES256 | 64 | 86 |
| RS256 with a 2048-bit key | 256 | 342 |
| RS256 with a 4096-bit key | 512 | 683 |
The payload is usually the largest part, and within it a few claims dominate: role lists, group lists, and user attributes added by mappers.
What this calculator shows
- The token’s size in bytes, and the size of each of its three parts.
- Its size as an
Authorization: Bearerheader line. - Its size as a cookie with the usual attributes, using the cookie name you enter.
- A comparison with the default limits of common software.
- The largest claims, with their share of the token, so you know what to trim.
Common limits
These are documented defaults. Each can be changed in the software’s configuration.
| Where | Default limit | Counts |
|---|---|---|
| Browser cookie | About 4096 bytes | One cookie: name, value and attributes (RFC 6265 asks browsers to allow at least this) |
| nginx | 8 KB | One request header line (large_client_header_buffers 4 8k) |
| Apache httpd | 8190 bytes | One request header field (LimitRequestFieldSize) |
| Tomcat | 8 KB | All request headers together (maxHttpHeaderSize) |
| Node.js | 16 KB | All request headers together (--max-http-header-size) |
Where the limit covers all headers, the token shares the space with cookies, the user agent and everything else in the request. When a limit is exceeded, servers typically answer with status 400 or 431.
How to measure a token
- Paste the token, ideally for the user with the most roles and groups.
- Enter the cookie name your app uses, if it stores the token in a cookie.
- Read the sizes and the limits table.
- Look at the largest claims to decide what to remove or move.
What makes Keycloak tokens grow
A Keycloak access token’s size depends on the user, the client and the client scopes.
Roles from every client
When Full scope allowed is on for a client, the token lists every realm role and every client role the user has, for every client. In a realm with many clients, resource_access becomes the biggest claim.
Groups
A group membership mapper writes every group path the user belongs to. Users in many groups produce long lists, especially with full paths.
User attributes and custom mappers
Each mapper that copies an attribute into the access token adds to every request, even when only one API needs the value.
How to make a Keycloak token smaller
- Turn off Full scope allowed on the client’s dedicated scope, and map only the roles the client needs.
- Remove client scopes the client does not use, or move them from default to optional so they apply only when requested.
- On mappers, turn off “Add to access token” for claims that only the client app needs in the ID token or userinfo.
- Use the group membership mapper without full paths if names are unique.
- Consider lightweight access tokens, available in recent Keycloak versions as a client setting, which leave most claims out; resource servers then read the full claims through introspection.
After each change, get a new token and measure again. The admin console can generate an example token for a user under the client’s Client scopes, Evaluate tab.
Use cases
Users who get “400 Bad Request” or “431”
If only some users see these errors, measure their tokens. Large group or role memberships are the usual cause.
Storing tokens in cookies
Before moving tokens into cookies, measure the largest one. If it does not fit in 4096 bytes with the cookie attributes, it needs trimming or a server-side session instead.
Choosing a signature algorithm
Switching from RS256 with a 4096-bit key to ES256 removes several hundred characters from every token.
A worked example
The sample token on this page, made up for the realm acme, is about 1.4 KB. It holds four realm roles and two clients’ roles. Here is what happens as a realm grows.
- Each extra realm role adds its name plus a few characters of JSON, times about 4/3 after encoding.
- Each client in
resource_accessadds its id, theroleskey and its role names. - A group membership mapper with full paths adds every path, such as
/departments/sales/emea.
A user with roles on many clients and dozens of groups can push a token past 4 KB without any single claim looking large. The largest-claims table shows where the bytes go, so you can fix the biggest contributor first.
Header limits along the way
A request passes several layers before it reaches your API, and each can have its own limit.
- The browser, which drops cookies larger than it allows.
- A CDN or load balancer, with its own header size limit.
- A reverse proxy such as nginx or Apache in front of the app.
- The application server or runtime, such as Tomcat or Node.js.
The smallest limit along the path is the one that counts. When you raise a limit, check every layer, and check the error each layer returns so you can tell which one rejected the request.
Choosing what to keep in the token
A useful test for each claim: does every API that receives this token need it on every request? Identity (sub), audience, scopes and the roles the API checks pass the test. Display names, addresses and long group lists usually do not. Move them to the ID token, the userinfo endpoint, or a lookup in your own database.
Limits of this calculator
The limits table lists defaults. Your proxy, load balancer or framework may be configured differently, and several layers may each apply their own limit. When in doubt, test with a large token through your real stack.
Claim sizes are estimated from their JSON and the Base64url growth; the exact share can differ by a few bytes.
Best practices
- Keep access tokens about authorization: identity, audience, scopes and the roles the API needs.
- Put profile data in the ID token or userinfo, not the access token.
- Measure tokens for your heaviest users, not your test user.
- Raise server header limits only after trimming what you can.