Skip to main content
PromptLayer takes less than 5 minutes to setup.
  • Sign up for an account at www.promptlayer.com
  • Grab an API key from PromptLayer. Find it in settings (click the cog on the top right).
It’s super easy to add PromptLayer to an existing project. The run() method fetches a prompt template from the Prompt Registry, executes it, and logs the result automatically.
# pip install promptlayer
from promptlayer import PromptLayer
promptlayer_client = PromptLayer()

response = promptlayer_client.run(
    prompt_name="my-prompt",
    input_variables={"variable": "value"}
)

Option 2: Use log_request for Custom Logging

If you want to keep using your existing LLM client code, use log_request to manually log requests to PromptLayer.
# pip install promptlayer
from openai import OpenAI
from promptlayer import PromptLayer
import time

pl_client = PromptLayer()
client = OpenAI()

messages = [{"role": "user", "content": "Hello!"}]

start = time.time()
completion = client.chat.completions.create(model="gpt-4o", messages=messages)
end = time.time()

pl_client.log_request(
    provider="openai", model="gpt-4o",
    input={"type": "chat", "messages": [{"role": m["role"], "content": [{"type": "text", "text": m["content"]}]} for m in messages]},
    output={"type": "chat", "messages": [{"role": "assistant", "content": [{"type": "text", "text": completion.choices[0].message.content}]}]},
    request_start_time=start, request_end_time=end
)
See the Custom Logging documentation for more details including format converters for OpenAI and Anthropic.