For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Help on Discord
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
    • Overview
  • baml-cli
    • init
    • generate
    • test
    • serve
    • dev
    • fmt
  • Language Reference
    • Types
    • function
    • test
    • template_string
    • client<llm>
    • class
    • enum
    • generator
  • Generated baml_client
    • with_options(..)
    • AbortSignal / Cancellation
    • Collector
    • logging / env vars
    • AsyncClient / SyncClient
    • TypeBuilder
    • ClientRegistry
    • client Option
    • OnTick
    • Multimodal
    • Image
    • Audio
    • Pdf
    • Video
  • Attributes
    • What are attributes?
    • @alias / @@alias
    • @description / @@description
    • @skip
    • @assert
    • @check
    • Jinja in Attributes
    • @@dynamic
  • LLM Client Providers
    • Overview
    • AWS Bedrock
    • Anthropic
    • Google AI: Gemini
    • Google: Vertex
    • OpenAI
    • OpenAI Responses API
    • OpenAI from Azure
    • OpenRouter
    • openai-generic
    • Microsoft Foundry (openai-generic)
    • Cerebras (openai-generic)
    • Groq (openai-generic)
    • Hugging Face (openai-generic)
    • Keywords AI (openai-generic)
    • Llama API (openai-generic)
    • Litellm (openai-generic)
    • LM Studio (openai-generic)
    • Ollama (openai-generic)
    • Vercel AI Gateway (openai-generic)
    • Tinfoil (openai-generic)
    • TogetherAI (openai-generic)
    • Unify AI (openai-generic)
    • vLLM (openai-generic)
  • LLM Client Strategies
    • Timeout Configuration
    • Retry Policy
    • Fallback
    • Round Robin
  • Prompt Syntax
    • What is jinja?
    • Jinja Filters
    • ctx.output_format
    • ctx.client
    • _.role
    • Variables
    • Conditionals
    • Loops
  • Editor Extension Settings
    • baml.cliPath
    • baml.generateCodeOnSave
    • baml.enablePlaygroundProxy
    • baml.syncExtensionToGeneratorVersion
Help on Discord
LogoLogo
On this page
  • Usecase: Conditionally render based on client provider
Prompt Syntax

ctx (accessing metadata)

Was this page helpful?
Edit this page
Previous

_.role

Next
Built with

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}