LangfuseLangfuse Python SDK 4.15.4 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
38 small wins to finish your pathNext lesson

Prompts kept in Langfuse

A prompt stored in Langfuse can change without a new deploy of your code. create_prompt saves a version, get_prompt fetches it, and compile fills in its variables.

The desk's instruction to the model, SYSTEM in desk.py, is a constant in the code. Changing a word means a code change and a release. Langfuse's prompt management keeps prompts on the server, with every version, so the text can change while the code stays the same.

Exampleprompt.py, after the setup lines and from desk import answer
langfuse.create_prompt(
    name="desk-system",
    type="text",
    prompt="You answer customers of {{shop}} in one short sentence.",
    labels=["production"],
)

type="text" makes the prompt one string; lesson 19 covers chat prompts. {{shop}} is a variable, filled in at run time. The production label marks this version as the one applications should use. Usually a person writes prompts in Langfuse's interface; the SDK call does the same.

Exampleprompt.py, continued
prompt = langfuse.get_prompt("desk-system")
print(prompt.version, prompt.labels, prompt.variables)
print(prompt.compile(shop="a small online shop"))
print(prompt.compile())
print(local_langfuse.REQUESTS)
Example
python prompt.py

Version 1 carries production, and latest, which Langfuse moves to the newest version by itself. get_prompt with no label asks for production. REQUESTS shows one request to store the prompt and one to fetch it.

The second compile is a bug that raises nothing. Without a value for shop, the variable stays in the text as {{shop}}, and the model would read the braces as they are. prompt.variables lists the names to fill; a test that compiles every prompt with your real values and checks that no {{ is left catches this before a customer does.

The desk with its prompt from Langfuse

Exampleprompt.py, continued
system = langfuse.get_prompt("desk-system").compile(shop="a small online shop")
print(answer("Where is my order A17?", system=system))
Example
python prompt.py

The compiled text goes in as the desk's system argument from lesson 9. The desk works as before; the difference is that the instruction can now be edited in Langfuse, and every version is kept.

Try it yourself
  • Pass shop="Ada's Books" and print the compiled text.
  • Fetch a prompt name that does not exist and read the error.
  • Add a second variable, {{tone}}, and print prompt.variables.

Slow is fine. Stopping is the only problem.