Server-side local evaluation
Note: Local evaluation is only available in the Node, Ruby, Go, Python, C#/.NET, PHP, Java, Rust, C/C++, and Elixir SDKs. It requires a secret API key, which must not be used in the frontend or exposed to users.
Note: Creating a client for each invocation in edge/Lambda environments or stateless PHP applications loses the default in-memory flag cache between requests. This adds fetches and cold-start latency. A shared module/process client retains its cache while that runtime lives. Use an external cache provider to share definitions across runtimes or retain them across runtime replacement, or use regular remote flag evaluation instead.
Evaluating feature flags requires making a request to PostHog for each flag. However, you can improve performance by evaluating flags locally. Instead of making a request for each flag, PostHog will periodically request and store feature flag definitions locally, enabling you to evaluate flags without making additional requests.
It is best practice to use local evaluation flags when possible, since this enables you to resolve flags faster and with fewer API calls.
How local evaluation works
To understand local evaluation, it helps to see how remote evaluation (the default) works first.
With remote evaluation, your server sends a request to PostHog's /flags API every time you check a flag. PostHog looks up all the data it needs — flag definitions, person properties, group properties, and cohort memberships — from its own database, evaluates the flag, and returns the result:
Every flag check is a network round trip. PostHog already has all the data it needs, so you just pass the flag key and a user ID.
With local evaluation, the SDK periodically fetches flag definitions from PostHog's /flags/definitions endpoint in the background. When you check a flag, you provide the properties and the SDK evaluates locally — no round trip:
Notice that PostHog is not involved at evaluation time. The tradeoff: you are responsible for providing every property the flag's release conditions depend on. If you forget to pass a property, the SDK can't evaluate the flag locally and will either fall back to a remote request or return undefined (depending on your configuration).
Local evaluation is also more cost-effective: with remote evaluation, every flag check is a billable request, whereas local evaluation only bills for the periodic poll for flag definitions (counted as 10 flag requests when the definitions changed, and 1 when they didn't). For high-traffic services, this can significantly reduce your flag usage costs.
Each flag definition includes a version number that the SDK uses to detect changes. PostHog increments this version whenever you modify the flag itself. It also increments the version when any cohort referenced by the flag — directly or through nested cohorts — has its conditions changed. This ensures your SDK cache stays in sync even when the flag's effective definition changes through a cohort update rather than a direct flag edit.
There are 3 steps to enable local evaluation:
Step 1: Get a secret API key
Initialize the SDK with your public project token. Server-side SDKs also need a secret API key for local evaluation and server-side remote config. This can be a project secret API key (recommended) or the legacy feature flags secure API key. Keep it secret: don't use it in frontend code or expose it to users.
We recommend using a project secret API key with the feature_flag:read scope. Project secret API keys are hashed at rest, scoped to only what they need, and you can create up to 50 per project, so you can issue and roll keys per deployment without affecting the others.
A personal API key is a private credential tied to your account for management API access. Source-map uploads use separate CLI authentication, not the feature_flag:read project secret.
Building a frontend, mobile, or CLI app? Local evaluation and the secret API key are for server-side use only. To evaluate flags from client-side code, use a client-side SDK or call the public
/flagsendpoint with your project token instead. The project token is safe to expose to users. Never include secret or personal API keys in distributed client code.
Note: Existing feature flags secure API keys (deprecated) and personal API keys continue to work. We recommend switching to a project secret API key. If you use encrypted payloads, keep a personal API key for those requests, since it's the only key that returns them decrypted.
How to create a project secret API key
Go to Project secret API keys in your project settings. You need project admin access to create a key.
Create a new key with the Local feature flag evaluation preset (the
feature_flag:readscope) and give it a label.Copy the key. It's only displayed once, so store it somewhere safe, like a secrets manager.
Pass it in the client's options, separately from the public project token. Use
secretKeyinposthog-node5.40.0+ orsecret_keyin synchronousposthog7.22.1+. Older versions usepersonalApiKeyorpersonal_api_key, respectively. These legacy options remain deprecated aliases in newer versions. The Java SDK usespersonalApiKey. The option name does not change the key's permissions.
Step 2: Initialize PostHog with your secret API key
When you initialize PostHog with your secret API key, PostHog will use the key to automatically fetch feature flag definitions. These definitions are then used to evaluate feature flags locally.
By default, PostHog fetches these definitions every 30 seconds (or 5 minutes in the Go SDK). However, you can change this frequency by specifying a different value in the polling interval argument.
Note: For billing purposes, we count a request that returns the feature flag definitions as being equivalent to
10 flagsrequests.This is because one of these requests can compute feature flags for hundreds or thousands of users. It ensures local evaluation is priced fairly while remaining the most cost-effective option (by far!).
Most polls don't return the definitions. The SDK sends the ETag of the definitions it already holds, and when nothing changed PostHog replies
304 Not Modifiedwith an empty body. We count that poll as 1 request. See reducing local evaluation costs for the SDK versions that send ETags.
Step 3: Evaluate your feature flag
To evaluate the feature flag, call the flag evaluation method for your SDK. In server-side APIs, evaluate flags once and read values from the returned snapshot. The only difference is that you must provide any person properties, groups, or group properties used to evaluate the release conditions of the flag. For a flag that uses device bucketing, you must also provide the device ID.
Then, by default, PostHog attempts to evaluate the flag locally using definitions it loads on initialization and at the poll interval. If this fails, PostHog then makes a server request to fetch the flag value.
You can disable this behavior by setting onlyEvaluateLocally to true. In this case, PostHog will only attempt to evaluate the flag locally, and return undefined / None / nil if it was unable to.
Cold start and fallback defaults
When the SDK first starts, it needs up to one polling interval (30 seconds by default) to fetch flag definitions. During this window, local evaluation returns undefined / None because no definitions are loaded yet. Use per-flag defaults to control what users see during this period:
If you deploy frequently or have many short-lived workers, consider using a shared flag definition cache so definitions survive restarts and the cold start window is eliminated.
Reloading flags
As mentioned in step 2, PostHog periodically fetches feature flag definitions. However, you can also force a reload by calling reloadFeatureFlags():
Restriction on local evaluation
General restrictions
Local evaluation is not possible for flags that:
Have experience continuity enabled, which is set when you check 'persist flag across authentication steps' on your feature flag.
Are linked to an early access feature, unless you pass the
$feature_enrollment/<flag key>person property to one of these SDK versions:These SDKs decide the flag from the property before they check its release conditions:
trueor"true"enables the flag. Any other value disables it.- Without the property, the SDK evaluates the flag through the
/flagsendpoint, or leaves it unresolved when you only evaluate locally. If you list the property as known absent, the SDK evaluates the flag's release conditions instead. - Older versions of these SDKs, and our Elixir SDK, ignore the property. They evaluate the flag's release conditions locally and don't fall back to
/flags, so people who opted in get the rollout result, which is usuallyfalse.
Depend on static cohorts
Use the
is_not_setproperty operator, since local evaluation can only check properties you provide – it can't determine whether a property is absent on a person. To evaluate these locally, list the keys you know are absent. See Known-absent property keys below.Have "Stop evaluation at first matching condition set" (early exit) enabled. This restriction does not apply to v2.15.0 and above of our Elixir SDK, which honors early exit during local evaluation. Other SDKs currently evaluate all condition groups locally, even when this setting is enabled. This is a temporary limitation while support is rolled out across SDKs. For those SDKs, the setting only takes effect when flags are evaluated through the
/flagsendpoint.Have a person-aggregated condition with a
groupproperty filter withoutgroup_type_index. In the C#/.NET SDK, local evaluation leaves the flag unresolved with theunsupported_definitionreason, so the flag falls back to the server.
Known-absent property keys
Note: Supported in v4.28.0 and above of our PHP SDK, v3.41.0 and above of our Ruby SDK, v8.9.0 and above of our Python SDK, and v2.10.0 and above of our Go SDK.
If you know a person or group doesn't have a property, pass its key as known absent. Local evaluation then treats the key as unset, even if you also pass a value for it:
is_not_set,is_not,not_icontains,not_starts_with,not_ends_with, andnot_regexmatch.- Every other operator doesn't match, including
semver_neq. - Operators the SDK can't evaluate locally, and conditions without a value, still fall back to the server.
Listed keys are also sent with any fallback server request. Each list can hold up to 1000 keys, per group type for groups.
Dynamic cohort restrictions
Note: This restriction does not apply to our Go SDK, Rust SDK, v2.6.0 and above of our Node SDK, and to v2.4.0 and above of our Python SDK.
To enable local evaluation of feature flags that depend on dynamic cohorts, we translate the cohort definition into person properties.
However, there are a few constraints, and cohorts cannot be evaluated locally if:
- There is a variant override on the condition with the cohort.
- They have non-person properties.
- There's more than one cohort in the feature flag definition.
- The cohort in the feature flag is in the same group as another condition.
- The cohort has nested AND-OR filters. In affected SDKs, only simple cohorts that have a top level OR group, and inner level ANDs can be evaluated locally. This restriction does not apply to v2.15.0 and above of our Elixir SDK, which supports nested AND/OR groups and negation during local evaluation.





