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.
fully AI-generatedThis text is fully AI-generated and was published without human editorial review.$ pip install -U langchain-anthropic==1.5.4- 1.5.4
- release
- PR #39148
- change
- instance attribute
- style
- multi-tenant
- use case
langchain-anthropic
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 freelymodel = 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_configmodel.with_config({...user_profile_id config...}) # alternative for per-call bindingCommon 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.
Under the hood
Where the value flows
Setting user_profile_id stores it as model state that is merged into every outgoing request\u2019s parameters. Because it rides the normal request path, it composes with everything else - bind_tools, streaming, with_config - without special handling. The attribute is purely a convenience layer over the existing API parameter.
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
- 1Set user_profile_id once per tenant-owned model instance.
- 2Use with_config when a single instance must serve multiple profiles.
- 3Audit your request tagging before scaling multi-tenant usage.
Related commands
archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →
