---
title: "Key cached data by every input · Agent Wranglers"
url: https://agentwranglers.se/playbook/app-ui/client-and-server-state/complete-cache-keys
updated: 2026-10-20
licence: "All rights reserved"
---

# Key cached data by every input

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Client and server state](https://agentwranglers.se/playbook/app-ui/client-and-server-state)
- Rule: Build every cache key from all the inputs that change the answer, including the account, the signed-in user, the filters, the sort and the page.
- How: Put the account or tenant first, so one call can drop all of its data. Then add the resource and each parameter. Pass the same values to the request and to the key from one object, so they can't drift apart. Clear the whole cache on sign-out and on switching accounts. The same rule holds for server-side caches, as [caches and copies](https://agentwranglers.se/playbook/data/caches-and-copies) explains.
- Check: A test signs in to account A, loads orders, switches to account B and loads orders. No request answer or rendered row from account A appears.

Example:

```ts
export const orderKeys = {
  all: (accountId: string) => ['accounts', accountId, 'orders'] as const,
  list: (accountId: string, filters: OrderFilters) => [...orderKeys.all(accountId), 'list', filters] as const,
};

export function useOrders(accountId: string, filters: OrderFilters) {
  return useQuery({ queryKey: orderKeys.list(accountId, filters), queryFn: () => api.listOrders(accountId, filters) });
}

export function onAccountSwitch(queryClient: QueryClient) {
  queryClient.clear();
}
```

```python
def orders_cache_key(account_id: str, filters: OrderFilters) -> str:
    return ":".join(["orders", account_id, filters.status or "any", filters.sort, filters.cursor or "first"])
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers app interface playbook: https://agentwranglers.se/playbook/app-ui/client-and-server-state/complete-cache-keys`.

- Previous: [Keep server data in one cache](https://agentwranglers.se/playbook/app-ui/client-and-server-state/server-state-cache)
- Next: [Show the answer to the latest request only](https://agentwranglers.se/playbook/app-ui/client-and-server-state/latest-request-wins)
- [Client and server state](https://agentwranglers.se/playbook/app-ui/client-and-server-state)
