ctx (accessing metadata)

If you try rendering {{ ctx }} into the prompt (literally just write that out!), you’ll see all the metadata we inject to run this prompt within the playground preview.

In the earlier tutorial we mentioned ctx.output_format, which contains the schema, but you can also access client information:

Usecase: Conditionally render based on client provider

In this example, we render the list of messages in XML tags if the provider is Anthropic (as they recommend using them as delimiters). See also template_string as it’s used in here.

1template_string RenderConditionally(messages: Message[]) #"
2 {% for message in messages %}
3 {%if ctx.client.provider == "anthropic" %}
4 <Message>{{ message.user_name }}: {{ message.content }}</Message>
5 {% else %}
6 {{ message.user_name }}: {{ message.content }}
7 {% endif %}
8 {% endfor %}
9"#
10
11function MyFuncWithGPT4(messages: Message[]) -> string {
12 client GPT4o
13 prompt #"
14 {{ RenderConditionally(messages)}}
15 "#
16}
17
18function MyFuncWithAnthropic(messages: Message[]) -> string {
19 client Claude35
20 prompt #"
21 {{ RenderConditionally(messages )}}
22 #"
23}