You can use the LangSmith Python and TypeScript SDK to manage prompts programmatically.
Previously this functionality lived in the langchainhub package which is now deprecated. All functionality going forward will live in the langsmith package.
In Python, you can directly use the LangSmith SDK (recommended, full functionality) or you can use through the LangChain package (limited to pushing and pulling prompts).In TypeScript, you must use the LangChain npm package for pulling prompts (it also allows pushing). For all other functionality, use the LangSmith package.
If you already have LANGSMITH_API_KEY set to your current workspace’s api key from LangSmith, you can skip this step.Otherwise, get an API key for your workspace by navigating to Settings > API Keys > Create API Key in LangSmith.Set your environment variable.
Copy
export LANGSMITH_API_KEY="lsv2_..."
What we refer to as “prompts” used to be called “repos”, so any references to “repo” in the code are referring to a prompt.
To create a new prompt or update an existing prompt, you can use the push prompt method.
Copy
from langsmith import Clientfrom langchain_core.prompts import ChatPromptTemplateclient = Client()prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")url = client.push_prompt("joke-generator", object=prompt)# url is a link to the prompt in the UIprint(url)
You can also push a prompt as a RunnableSequence of a prompt and a model. This is useful for storing the model configuration you want to use with this prompt. The provider must be supported by the LangSmith playground. (see settings here: Supported Providers)
Copy
from langsmith import Clientfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_openai import ChatOpenAIclient = Client()model = ChatOpenAI(model="gpt-4.1-mini")prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")chain = prompt | modelclient.push_prompt("joke-generator-with-model", object=chain)
To pull a prompt, you can use the pull prompt method, which returns a the prompt as a langchain PromptTemplate.To pull a private prompt you do not need to specify the owner handle (though you can, if you have one set).To pull a public prompt from the LangChain Hub, you need to specify the handle of the prompt’s author.
Similar to pushing a prompt, you can also pull a prompt as a RunnableSequence of a prompt and a model. Just specify include_model when pulling the prompt. If the stored prompt includes a model, it will be returned as a RunnableSequence. Make sure you have the proper environment variables set for the model you are using.
Copy
from langsmith import Clientclient = Client()chain = client.pull_prompt("joke-generator-with-model", include_model=True)chain.invoke({"topic": "cats"})
When pulling a prompt, you can also specify a specific commit hash or commit tag to pull a specific version of the prompt.
For pulling prompts, if you are using Node.js or an environment that supports dynamic imports, we recommend using the langchain/hub/node entrypoint, as it handles deserialization of models associated with your prompt configuration automatically.If you are in a non-Node environment, “includeModel” is not supported for non-OpenAI models and you should use the base langchain/hub entrypoint.
The LangSmith SDK includes built-in in-memory caching for prompts. When enabled, LangSmith will cache pulled prompts in memory, reducing latency and API calls for frequently used prompts. The cache uses a global singleton instance that is shared across all clients and persists for the lifetime of the process. It implements a stale-while-revalidate pattern, ensuring your application always gets a fast response while keeping prompts up-to-date in the background.Requirements:
By default, all clients use the global prompt cache. No configuration is needed:
Copy
from langsmith import Client# Obtain a reference to the global cache just for logging metricsfrom langsmith.prompt_cache import prompt_cache_singleton# Caching is enabled by default using the global singletonclient = Client()# First pull - fetches from API and cachesprompt = client.pull_prompt("joke-generator")# Subsequent pulls - returns cached version instantlyprompt = client.pull_prompt("joke-generator")# Check cache metricsprint(f"Cache hits: {prompt_cache_singleton.metrics.hits}")print(f"Cache misses: {prompt_cache_singleton.metrics.misses}")print(f"Hit rate: {prompt_cache_singleton.metrics.hit_rate:.1%}")
You can configure the global prompt cache that all clients use by default. This is useful when you want to customize caching behavior across your entire application:
Copy
from langsmith import Clientfrom langsmith.prompt_cache import ( configure_global_prompt_cache, prompt_cache_singleton,)# Configure global cache before creating any clientsconfigure_global_prompt_cache( max_size=200, # Cache up to 200 prompts ttl_seconds=7200, # Consider prompts stale after 2 hours refresh_interval_seconds=600, # Check for stale prompts every 10 minutes)# All clients will use these settingsclient1 = Client()client2 = Client()# Both clients share the same global cache with your custom settingsprompt1 = client1.pull_prompt("prompt-1")prompt2 = client2.pull_prompt("prompt-2")# Check global cache metricsprint(f"Global cache hits: {prompt_cache_singleton.metrics.hits}")print(f"Global cache misses: {prompt_cache_singleton.metrics.misses}")
To disable caching for a specific client, pass disable_prompt_cache=True. You can also configure a max size of zero globally:
Copy
from langsmith import Client# Disable caching for this clientclient = Client(disable_prompt_cache=True)# Every pull will fetch from the APIprompt = client.pull_prompt("joke-generator")
For environments with limited or no network connectivity, you can pre-populate the cache and use it offline. Set ttl_seconds to None (Python) or null (TypeScript) to prevent cache entries from expiring and disable background refresh.Step 1: Export your prompts to a cache file (while online)
Copy
from langsmith import Clientfrom langsmith.prompt_cache import prompt_cache_singleton# Create client (caching is enabled by default)client = Client()# Pull the prompts you needclient.pull_prompt("prompt-1")client.pull_prompt("prompt-2")client.pull_prompt("prompt-3")# Export cache to a fileprompt_cache_singleton.dump("prompts_cache.json")
Step 2: Load the cache file in your offline environment
Copy
from langsmith import Clientfrom langsmith.prompt_cache import ( configure_global_prompt_cache, prompt_cache_singleton,)# Configure cache with infinite TTL (never expire, no background refresh)configure_global_prompt_cache(ttl_seconds=None)# Load the cache fileprompt_cache_singleton.load("prompts_cache.json")# Create client (uses the loaded cache)client = Client()# Uses cached version without any API callsprompt = client.pull_prompt("prompt-1")
The cache supports several operations for managing cached prompts:
Copy
from langsmith import Clientfrom langsmith.prompt_cache import prompt_cache_singletonclient = Client()# Invalidate a specific prompt from cacheprompt_cache_singleton.invalidate("joke-generator:latest")# Clear all cached promptsprompt_cache_singleton.clear()# Reset metricsprompt_cache_singleton.reset_metrics()# Check if cache is running background refresh# (only runs if ttl_seconds is not None)if prompt_cache_singleton._refresh_thread is not None: print("Background refresh is active")
You can manually call stop() to stop the background refresh task:
Copy
prompt_cache_singleton.stop()
The background refresh task is only started when you first set a value in the cache, and only if ttl_seconds is not None. If ttl_seconds is None (offline mode), no background task is created.
If you want to store your prompts in LangSmith but use them directly with a model provider’s API, you can use our conversion methods. These convert your prompt into the payload required for the OpenAI or Anthropic API.These conversion methods rely on logic from within LangChain integration packages, and you will need to install the appropriate package as a dependency in addition to your official SDK of choice. Here are some examples:
You can also list, delete, and like/unlike prompts using the list prompts, delete prompt, like prompt and unlike prompt methods. See the LangSmith SDK client for extensive documentation on these methods.
Copy
# List all prompts in my workspaceprompts = client.list_prompts()# List my private prompts that include "joke"prompts = client.list_prompts(query="joke", is_public=False)# Delete a promptclient.delete_prompt("joke-generator")# Like a promptclient.like_prompt("efriis/my-first-prompt")# Unlike a promptclient.unlike_prompt("efriis/my-first-prompt")