Skip to main content

langchain-prefid

Official LangChain integration for PrefID - Build preference-aware AI agents that adapt to user preferences

PyPI Python 3.9+ License: MIT

What is PrefID?

PrefID is identity-aware AI memory infrastructure that helps AI agents understand:

  • WHAT users like (food, music, travel preferences)
  • HOW users want responses (thinking preferences - brevity, detail, structure)

Installation

pip install langchain-prefid

Quick Start

from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_prefid import create_prefid_tools

# Create PrefID tools
tools = create_prefid_tools(
    client_id="your-prefid-client-id",
    access_token="user-access-token",
    user_id="user_123"
)

# Create agent
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant. Use the preference tools to personalize responses."),
    ("placeholder", "{chat_history}"),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)

# Use it!
result = executor.invoke({
    "input": "Recommend a restaurant for tonight"
})

Available Tools

1. get_user_preferences

Get user preferences for any domain (food, music, travel, coding, etc.)

# Agent will call this tool
"Get user's food preferences to recommend restaurants"

2. get_thinking_preferences

Get HOW the user wants AI to respond (Atom of Thought)

# Returns:
# - Reasoning style (stepwise, summary_first)
# - Decision style (recommend, tradeoffs, options)
# - Verbosity (concise, detailed, examples)
# - Autonomy (proactive, confirm_first)

3. learn_thinking_preference

Learn and remember how the user wants responses

# When user says: "I prefer brief answers"
# Agent calls this tool to remember it

4. explain_response_style

Introspection - explain why AI is responding a certain way

# When user asks: "Why are you responding like this?"
# Agent explains active preferences

Example Agent

from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_prefid import create_prefid_tools

# Setup
tools = create_prefid_tools(
    client_id="...",
    access_token="...",
    user_id="user_123"
)

llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")

system_prompt = """You are a restaurant recommendation assistant.

BEFORE recommending:
1. Call get_thinking_preferences() to learn HOW to respond
2. Call get_user_preferences('food_profile') to learn WHAT they like
3. Structure response according to their preferences

If user asks why you responded that way, call explain_response_style().
If user tells you how they prefer responses, call learn_thinking_preference().
"""

prompt = ChatPromptTemplate.from_messages([
    ("system", system_prompt),
    ("placeholder", "{chat_history}"),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Run
response = executor.invoke({
    "input": "Where should I eat tonight?"
})

Individual Tools

You can also use tools individually:

from langchain_prefid import (
    PrefIDPreferenceTool,
    PrefIDThinkingTool,
    PrefIDLearnTool,
    PrefIDWhyTool
)
from prefid import PrefID

prefid = PrefID(client_id="...", access_token="...")

# Create individual tools
pref_tool = PrefIDPreferenceTool(prefid_client=prefid)
thinking_tool = PrefIDThinkingTool(prefid_client=prefid)
learn_tool = PrefIDLearnTool(prefid_client=prefid)
why_tool = PrefIDWhyTool(prefid_client=prefid)

# Use them
food_prefs = pref_tool.invoke({"domain": "food_profile"})
thinking = thinking_tool.invoke({"user_id": "user_123"})

Key Concepts

Atom of Thought (AoT)

PrefID's unique feature - learns HOW users want AI to respond:

  • "I prefer step-by-step explanations" → Reasoning: stepwise
  • "Just give me one recommendation" → Decision: recommend
  • "Keep it brief" → Verbosity: concise

This goes beyond WHAT users like to HOW they think.

Thinking Preferences vs Content Preferences

  • Content: What music/food/travel they like
  • Thinking: How they want information structured

Both are important for truly personalized AI.

Examples

See /examples for complete working examples:

  • restaurant_recommender.py - Full agent with all tools
  • basic_agent.py - Simple preference-aware agent

LangSmith Hub

Standard prompts for PrefID agents are available on LangSmith Hub:

Prompt Description
talentxmdu/restaurant-recommender Full agent prompt with AoT + Preference logic
talentxmdu/basic-agent Minimal prompt for preference aware agents

Usage:

from langchain import hub
prompt = hub.pull("talentxmdu/restaurant-recommender")

Get PrefID Credentials

  1. Sign up at prefid.in
  2. Create an OAuth client in Dashboard → Connected Apps
  3. Get your client_id and client_secret

Links

License

MIT © PrefID Team

Metadata

Release files for langchain-prefid 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-prefid 0.2.0
File Size Uploaded
langchain_prefid-0.2.0.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-prefid 0.2.0
File Interpreter ABI Platform
langchain_prefid-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / langchain_prefid-0.2.0.tar.gz

Download URL langchain_prefid-0.2.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
366c9e5de455bd9ff73675a850ed8fabad4d86309d578017fc920bd706e4a925
BLAKE2b-256 checksum
How to use checksums
28c6eaa436b14cf82b88acbedc74ac041aa899bca90baf0f0291ffa9e0e7bd24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / langchain_prefid-0.2.0-py3-none-any.whl

Download URL langchain_prefid-0.2.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dcc490a8f9f6a1285c929757a16b5362d6f2de5c450903f032b6606e57d537a7
BLAKE2b-256 checksum
How to use checksums
ba64b435f75262a783dac224f97172e7bb0740bc13564da6d77aabe0fb7d328d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page