kmail.at
← learning

langchain · difficulty ◆◆

user_profile_id as a Convenience Attribute on ChatAnthropic

Tag every call for the right tenant without threading an argument through each invoke.

The most multi-tenant-friendly feature is one you set once and forget - exactly what user_profile_id is now.

2026-08-07 · 6 min read

$ pip install -U langchain-anthropic==1.5.4

What it does

user_profile_id is a new convenience attribute on the ChatAnthropic wrapper. It is a shorthand way to set the user_profile_id parameter Anthropic\u2019s API uses to associate requests with a specific user profile for billing, analytics, and access control. Instead of passing user_profile_id= on every call or configuring it via an environment variable, you set it once on the model instance after initialization, and all subsequent calls are tagged.

Why it matters

Multi-tenant systems serving many end-users need each request tagged with the correct user_profile_id for attribution. Previously you had to pass it manually per call or juggle a separate configuration object. The attribute lets you set it once on a model reused across many requests for the same profile - much cleaner for multi-tenant LLM apps where each tenant\u2019s calls must be tracked separately.

Example

$ Set the attribute once and call freely
model = ChatAnthropic(model="claude-sonnet-4-20250514")
model.user_profile_id = "tenant_acme_corp"
response = model.invoke(messages)
A concise summary of Q3 earnings for ACME Corp, covering revenue, key metrics, and highlights.

Every call on this model instance is now tagged with tenant_acme_corp - no per-call argument needed.

$ Bind fixed config across many calls with with_config
model.with_config({...user_profile_id config...})  # alternative for per-call binding

Common flags

user_profile_id
Attribute tagging requests with a user profile for attribution.
with_config
Bind fixed config across multiple calls.
invoke
Synchronously invoke the model with a list of messages.

History

From per-call arguments to instance state

Anthropic\u2019s user_profile_id has always existed in the API, but LangChain exposed it awkwardly - either per-call or through config plumbing. The convenience attribute makes it instance state, matching how developers naturally think: \u201cthis model belongs to this tenant\u201d. It is a small ergonomic change with real payoff for multi-tenant code.

Fun facts

Pros & cons

pros

  • + Set-once ergonomics
  • + Multi-tenant billing and analytics
  • + Composes with config and tools

cons

  • − Anthropic-specific attribute
  • − Not a substitute for a proper tenancy layer

Takeaways

  1. 1Set user_profile_id once per tenant-owned model instance.
  2. 2Use with_config when a single instance must serve multiple profiles.
  3. 3Audit your request tagging before scaling multi-tenant usage.

Related commands

← all learning