> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-javaex-1765204202-a1f8093.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage prompts programmatically

You can use the LangSmith Python, TypeScript, and Java SDKs to manage prompts programmatically.

<Note>
  Previously this functionality lived in the `langchainhub` package which is now deprecated. All functionality going forward will live in the `langsmith` package.
</Note>

## Install packages

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.

In Java, use the LangSmith SDK to manage prompts through the repos and commits API.

<CodeGroup>
  ```bash pip theme={null}
  pip install -U langsmith # version >= 0.1.99
  ```

  ```bash uv theme={null}
  uv add langsmith  # version >= 0.1.99
  ```

  ```bash TypeScript theme={null}
  yarn add langsmith langchain // langsmith version >= 0.1.99 and langchain version >= 0.2.14
  ```

  ```xml Maven theme={null}
  <dependency>
    <groupId>com.langchain.smith</groupId>
    <artifactId>langsmith-java</artifactId>
    <version>0.1.0-alpha.17</version>
  </dependency>
  ```

  ```kotlin Gradle theme={null}
  implementation("com.langchain.smith:langsmith-java:0.1.0-alpha.17")
  ```
</CodeGroup>

## Configure environment variables

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.

```bash theme={null}
export LANGSMITH_API_KEY="lsv2_..."
```

<Note>
  What we refer to as "prompts" used to be called "repos", so any references to "repo" in the code are referring to a prompt.
</Note>

## Push a prompt

To create a new prompt or update an existing prompt, you can use the `push prompt` method.

<CodeGroup>
  ```python Python theme={null}
  from langsmith import Client
  from langchain_core.prompts import ChatPromptTemplate

  client = 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 UI
  print(url)
  ```

  ```python LangChain (Python) theme={null}
  from langchain_classic import hub as prompts
  from langchain_core.prompts import ChatPromptTemplate

  prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
  url = prompts.push("joke-generator", prompt)
  # url is a link to the prompt in the UI
  print(url)
  ```

  ```typescript TypeScript theme={null}
  import * as hub from "langchain/hub";
  import { ChatPromptTemplate } from "@langchain/core/prompts";

  const prompt = ChatPromptTemplate.fromTemplate("tell me a joke about {topic}");
  const url = hub.push("joke-generator", {
    object: prompt,
  });
  // url is a link to the prompt in the UI
  console.log(url);
  ```

  ```java Java theme={null}
  import com.langchain.smith.client.LangsmithClient;
  import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
  import com.langchain.smith.core.JsonValue;
  import com.langchain.smith.models.commits.CommitUpdateParams;
  import com.langchain.smith.models.repos.Repo;
  import com.langchain.smith.models.repos.RepoCreate;
  import java.util.Map;

  public class PromptManagement {
      public static void main(String[] args) {
          LangsmithClient client = LangsmithOkHttpClient.fromEnv();

          // Create a prompt repository
          Repo repo = client.repos().create(RepoCreate.builder()
              .repoHandle("joke-generator")
              .description("A prompt for generating jokes")
              .isPublic(false)
              .build());

          // Define prompt content
          Map<String, Object> promptManifest = Map.of(
              "template", "tell me a joke about {topic}",
              "input_variables", new String[]{"topic"}
          );

          // Push prompt content as a commit
          client.commits().update(
              repo.repoHandle(),
              CommitUpdateParams.builder()
                  .manifest(JsonValue.from(promptManifest))
                  .build()
          );

          System.out.println("Prompt created: https://smith.langchain.com/prompts/" + repo.repoHandle());
      }
  }
  ```
</CodeGroup>

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](https://langsmith.com/playground))

<CodeGroup>
  ```python Python theme={null}
  from langsmith import Client
  from langchain_core.prompts import ChatPromptTemplate
  from langchain_openai import ChatOpenAI

  client = Client()
  model = ChatOpenAI(model="gpt-4o-mini")
  prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
  chain = prompt | model
  client.push_prompt("joke-generator-with-model", object=chain)
  ```

  ```python LangChain (Python) theme={null}
  from langchain_classic import hub as prompts
  from langchain_core.prompts import ChatPromptTemplate
  from langchain_openai import ChatOpenAI

  model = ChatOpenAI(model="gpt-4o-mini")
  prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
  chain = prompt | model
  url = prompts.push("joke-generator-with-model", chain)
  # url is a link to the prompt in the UI
  print(url)
  ```

  ```typescript TypeScript theme={null}
  import * as hub from "langchain/hub";
  import { ChatPromptTemplate } from "@langchain/core/prompts";
  import { ChatOpenAI } from "@langchain/openai";

  const model = new ChatOpenAI({ model: "gpt-4o-mini" });
  const prompt = ChatPromptTemplate.fromTemplate("tell me a joke about {topic}");
  const chain = prompt.pipe(model);
  await hub.push("joke-generator-with-model", {
    object: chain,
  });
  ```
</CodeGroup>

## Pull a prompt

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.

<CodeGroup>
  ```python Python theme={null}
  from langsmith import Client
  from langchain_openai import ChatOpenAI

  client = Client()
  prompt = client.pull_prompt("joke-generator")
  model = ChatOpenAI(model="gpt-4o-mini")
  chain = prompt | model
  chain.invoke({"topic": "cats"})
  ```

  ```python LangChain (Python) theme={null}
  from langchain_classic import hub as prompts
  from langchain_openai import ChatOpenAI

  prompt = prompts.pull("joke-generator")
  model = ChatOpenAI(model="gpt-4o-mini")
  chain = prompt | model
  chain.invoke({"topic": "cats"})
  ```

  ```typescript TypeScript theme={null}
  import * as hub from "langchain/hub";
  import { ChatOpenAI } from "@langchain/openai";

  const prompt = await hub.pull("joke-generator");
  const model = new ChatOpenAI({ model: "gpt-4o-mini" });
  const chain = prompt.pipe(model);
  await chain.invoke({"topic": "cats"});
  ```

  ```java Java theme={null}
  import com.langchain.smith.client.LangsmithClient;
  import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
  import com.langchain.smith.models.commits.Commit;

  public class PullPrompt {
      public static void main(String[] args) {
          LangsmithClient client = LangsmithOkHttpClient.fromEnv();

          // Pull the latest commit for the prompt
          Commit commit = client.commits().get("joke-generator", "latest");

          // Access the prompt manifest
          System.out.println("Prompt manifest: " + commit.manifest());

          // Use the prompt content with your model
          // Extract template and use it with your preferred model provider
      }
  }
  ```
</CodeGroup>

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.

<CodeGroup>
  ```python Python theme={null}
  from langsmith import Client

  client = Client()
  chain = client.pull_prompt("joke-generator-with-model", include_model=True)
  chain.invoke({"topic": "cats"})
  ```

  ```python LangChain (Python) theme={null}
  from langchain_classic import hub as prompts

  chain = prompts.pull("joke-generator-with-model", include_model=True)
  chain.invoke({"topic": "cats"})
  ```

  ```typescript TypeScript theme={null}
  import * as hub from "langchain/hub";
  import { Runnable } from "@langchain/core/runnables";

  const chain = await hub.pull<Runnable>("joke-generator-with-model", { includeModel: true });
  await chain.invoke({"topic": "cats"});
  ```

  ```java Java theme={null}
  import com.langchain.smith.client.LangsmithClient;
  import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
  import com.langchain.smith.models.commits.Commit;

  public class PullPromptWithModel {
      public static void main(String[] args) {
          LangsmithClient client = LangsmithOkHttpClient.fromEnv();

          // Pull the latest commit for the prompt with model
          Commit commit = client.commits().get("joke-generator-with-model", "latest");

          // Access the prompt manifest which includes model configuration
          System.out.println("Prompt with model manifest: " + commit.manifest());

          // Extract model configuration and use with your preferred model provider
      }
  }
  ```
</CodeGroup>

When pulling a prompt, you can also specify a specific commit hash or [commit tag](/langsmith/manage-prompts#commit-tags) to pull a specific version of the prompt.

<CodeGroup>
  ```python Python theme={null}
  prompt = client.pull_prompt("joke-generator:12344e88")
  ```

  ```python LangChain (Python) theme={null}
  prompt = prompts.pull("joke-generator:12344e88")
  ```

  ```typescript TypeScript theme={null}
  const prompt = await hub.pull("joke-generator:12344e88")
  ```

  ```java Java theme={null}
  // Pull a specific commit by hash
  Commit commit = client.commits().get("joke-generator", "12344e88");
  ```
</CodeGroup>

To pull a public prompt from the LangChain Hub, you need to specify the handle of the prompt's author.

<CodeGroup>
  ```python Python theme={null}
  prompt = client.pull_prompt("efriis/my-first-prompt")
  ```

  ```python LangChain (Python) theme={null}
  prompt = prompts.pull("efriis/my-first-prompt")
  ```

  ```typescript TypeScript theme={null}
  const prompt = await hub.pull("efriis/my-first-prompt")
  ```

  ```java Java theme={null}
  // Pull a public prompt by specifying the owner handle
  Commit commit = client.commits().get("efriis/my-first-prompt", "latest");
  ```
</CodeGroup>

<Note>
  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.
</Note>

## Use a prompt without LangChain

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:

### OpenAI

<CodeGroup>
  ```bash Python theme={null}
  pip install -U langchain_openai
  ```

  ```bash TypeScript theme={null}
  yarn add @langchain/openai @langchain/core // @langchain/openai version >= 0.3.2
  ```
</CodeGroup>

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI
  from langsmith.client import Client, convert_prompt_to_openai_format

  # langsmith client
  client = Client()
  # openai client
  oai_client = OpenAI()

  # pull prompt and invoke to populate the variables
  prompt = client.pull_prompt("joke-generator")
  prompt_value = prompt.invoke({"topic": "cats"})
  openai_payload = convert_prompt_to_openai_format(prompt_value)
  openai_response = oai_client.chat.completions.create(**openai_payload)
  ```

  ```typescript TypeScript theme={null}
  import * as hub from "langchain/hub";
  import { convertPromptToOpenAI } from "@langchain/openai";
  import OpenAI from "openai";

  const prompt = await hub.pull("jacob/joke-generator");
  const formattedPrompt = await prompt.invoke({
    topic: "cats",
  });
  const { messages } = convertPromptToOpenAI(formattedPrompt);

  const openAIClient = new OpenAI();
  const openAIResponse = await openAIClient.chat.completions.create({
    model: "gpt-4o-mini",
    messages,
  });
  ```
</CodeGroup>

### Anthropic

<CodeGroup>
  ```bash Python theme={null}
  pip install -U langchain_anthropic
  ```

  ```bash TypeScript theme={null}
  yarn add @langchain/anthropic @langchain/core // @langchain/anthropic version >= 0.3.3
  ```
</CodeGroup>

<CodeGroup>
  ```python Python theme={null}
  from anthropic import Anthropic
  from langsmith.client import Client, convert_prompt_to_anthropic_format

  # langsmith client
  client = Client()
  # anthropic client
  anthropic_client = Anthropic()

  # pull prompt and invoke to populate the variables
  prompt = client.pull_prompt("joke-generator")
  prompt_value = prompt.invoke({"topic": "cats"})
  anthropic_payload = convert_prompt_to_anthropic_format(prompt_value)
  anthropic_response = anthropic_client.messages.create(**anthropic_payload)
  ```

  ```typescript TypeScript theme={null}
  import * as hub from "langchain/hub";
  import { convertPromptToAnthropic } from "@langchain/anthropic";
  import Anthropic from "@anthropic-ai/sdk";

  const prompt = await hub.pull("jacob/joke-generator");
  const formattedPrompt = await prompt.invoke({
    topic: "cats",
  });
  const { messages, system } = convertPromptToAnthropic(formattedPrompt);

  const anthropicClient = new Anthropic();
  const anthropicResponse = await anthropicClient.messages.create({
    model: "claude-haiku-4-5-20251001",
    system,
    messages,
    max_tokens: 1024,
    stream: false,
  });
  ```
</CodeGroup>

## List, delete, and like prompts

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](https://github.com/langchain-ai/langsmith-sdk) for extensive documentation on these methods.

<CodeGroup>
  ```python Python theme={null}
  # List all prompts in my workspace
  prompts = client.list_prompts()

  # List my private prompts that include "joke"
  prompts = client.list_prompts(query="joke", is_public=False)

  # Delete a prompt
  client.delete_prompt("joke-generator")

  # Like a prompt
  client.like_prompt("efriis/my-first-prompt")

  # Unlike a prompt
  client.unlike_prompt("efriis/my-first-prompt")
  ```

  ```typescript TypeScript theme={null}
  // List all prompts in my workspace
  import Client from "langsmith";

  const client = new Client({ apiKey: "lsv2_..." });
  const prompts = client.listPrompts();

  for await (const prompt of prompts) {
    console.log(prompt);
  }

  // List my private prompts that include "joke"
  const private_joke_prompts = client.listPrompts({ query: "joke", isPublic: false});

  // Delete a prompt
  client.deletePrompt("joke-generator");

  // Like a prompt
  client.likePrompt("efriis/my-first-prompt");

  // Unlike a prompt
  client.unlikePrompt("efriis/my-first-prompt");
  ```

  ```java Java theme={null}
  import com.langchain.smith.client.LangsmithClient;
  import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
  import com.langchain.smith.models.repos.Repo;
  import com.langchain.smith.models.repos.ReposListParams;

  public class ManagePrompts {
      public static void main(String[] args) {
          LangsmithClient client = LangsmithOkHttpClient.fromEnv();

          // List all prompts in my workspace
          Iterable<Repo> prompts = client.repos().list();
          for (Repo prompt : prompts) {
              System.out.println(prompt.repoHandle());
          }

          // List private prompts that include "joke"
          Iterable<Repo> jokePrompts = client.repos().list(
              ReposListParams.builder()
                  .query("joke")
                  .isPublic(false)
                  .build()
          );

          // Delete a prompt
          client.repos().delete("joke-generator");

          // Like a prompt
          client.repos().like("efriis/my-first-prompt");

          // Unlike a prompt
          client.repos().unlike("efriis/my-first-prompt");
      }
  }
  ```
</CodeGroup>

***

<Callout icon="pen-to-square" iconType="regular">
  [Edit the source of this page on GitHub.](https://github.com/langchain-ai/docs/edit/main/src/langsmith/manage-prompts-programmatically.mdx)
</Callout>

<Tip icon="terminal" iconType="regular">
  [Connect these docs programmatically](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
</Tip>
