How to beginner · 3 min read

How to manage prompts in LangSmith

Quick answer
Use the langsmith Python SDK to create, version, and organize prompts by defining prompt templates and tracking their usage. LangSmith enables prompt management with automatic tracing and metadata support for better prompt lifecycle control.

PREREQUISITES

  • Python 3.8+
  • LangSmith API key
  • pip install langsmith

Setup

Install the langsmith Python package and set your API key as an environment variable to authenticate your requests.

bash
pip install langsmith

Step by step

This example shows how to initialize the LangSmith client, create a prompt template, and log prompt usage with metadata for versioning and tracking.

python
import os
from langsmith import Client, PromptTemplate

# Set your LangSmith API key in environment variable LANGSMITH_API_KEY
client = Client(api_key=os.environ["LANGSMITH_API_KEY"])

# Define a prompt template
prompt_template = PromptTemplate(
    name="GreetingPrompt",
    template="Hello, {name}! Welcome to LangSmith prompt management.",
    description="A simple greeting prompt with a name placeholder."
)

# Create or update the prompt template in LangSmith
created_prompt = client.prompts.create_or_update(prompt_template)
print(f"Prompt template created with ID: {created_prompt.id}")

# Log prompt usage with input variables
usage = client.prompt_usages.create(
    prompt_id=created_prompt.id,
    inputs={"name": "Alice"},
    outputs={"text": "Hello, Alice! Welcome to LangSmith prompt management."},
    metadata={"version": "1.0", "purpose": "demo"}
)
print(f"Logged prompt usage with ID: {usage.id}")
output
Prompt template created with ID: prompt_1234567890abcdef
Logged prompt usage with ID: usage_abcdef1234567890

Common variations

  • Use async methods with await for asynchronous prompt management.
  • Manage multiple prompt versions by updating the metadata field with version info.
  • Integrate LangSmith prompt tracking with LangChain or other frameworks by wrapping prompt calls.
python
import asyncio
from langsmith import AsyncClient, PromptTemplate

async def async_prompt_management():
    client = AsyncClient(api_key=os.environ["LANGSMITH_API_KEY"])

    prompt_template = PromptTemplate(
        name="AsyncGreetingPrompt",
        template="Hi, {name}! This is async prompt management.",
        description="Async example prompt."
    )

    created_prompt = await client.prompts.create_or_update(prompt_template)
    print(f"Async prompt created with ID: {created_prompt.id}")

    usage = await client.prompt_usages.create(
        prompt_id=created_prompt.id,
        inputs={"name": "Bob"},
        outputs={"text": "Hi, Bob! This is async prompt management."},
        metadata={"version": "1.1", "async": True}
    )
    print(f"Logged async prompt usage with ID: {usage.id}")

asyncio.run(async_prompt_management())
output
Async prompt created with ID: prompt_abcdef1234567890
Logged async prompt usage with ID: usage_1234567890abcdef

Troubleshooting

  • If you see authentication errors, verify your LANGSMITH_API_KEY environment variable is set correctly.
  • For prompt creation failures, ensure the prompt template fields name and template are not empty.
  • Check network connectivity if API calls time out or fail.

Key Takeaways

  • Use the LangSmith Python SDK to create and version prompt templates programmatically.
  • Log prompt usage with inputs, outputs, and metadata for full prompt lifecycle tracking.
  • Leverage async SDK methods for scalable prompt management in asynchronous applications.
Verified 2026-04
Verify ↗