:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(aio): show prompt config usage in code snippets (#75847)
Juraj Majerikad6c17b06109
diff --git a/products/ai_observability/frontend/prompts/promptSceneComponents.tsx b/products/ai_observability/frontend/prompts/promptSceneComponents.tsxindex d43e32ac5251..712ed1f404bd 100644--- a/products/ai_observability/frontend/prompts/promptSceneComponents.tsx+++ b/products/ai_observability/frontend/prompts/promptSceneComponents.tsx@@ -530,11 +530,15 @@ function buildPythonSnippet(host: string,projectApiKey: string,variables: string[],- labelName?: string+ labelName?: string,+ showConfig?: boolean): string {const compileLines = variables.length? `\nsystem_prompt = prompts.compile(result.prompt, {${variables.map((v) => `${JSON.stringify(v)}: '...'`).join(', ')}})`: ''+ const configLines = showConfig+ ? `\nmodel = (result.config or {}).get('model', 'your-default-model') # config stored with this prompt version`+ : ''const labelArg = labelName ? `label=${JSON.stringify(labelName)}, ` : ''return `from posthog import Posthogfrom posthog.ai.prompts import Prompts@@ -546,7 +550,7 @@ posthog = Posthog()prompts = Prompts(posthog)-result = prompts.get(${JSON.stringify(promptName)}, ${labelArg}with_metadata=True, fallback='You are a helpful assistant.')${compileLines}+result = prompts.get(${JSON.stringify(promptName)}, ${labelArg}with_metadata=True, fallback='You are a helpful assistant.')${compileLines}${configLines}# result.name / result.version -> send as $ai_prompt_name / $ai_prompt_version on your LLM events`}@@ -555,11 +559,15 @@ function buildNodeSnippet(host: string,projectApiKey: string,variables: string[],- labelName?: string+ labelName?: string,+ showConfig?: boolean): string {const compileLines = variables.length? `\nconst systemPrompt = prompts.compile(result.prompt, {${variables.map((v) => ` ${JSON.stringify(v)}: '...'`).join(',')} })`: ''+ const configLines = showConfig+ ? `\nconst model = result.config?.model ?? 'your-default-model' // config stored with this prompt version`+ : ''const labelArg = labelName ? `label: '${labelName}', ` : ''return `import { Prompts } from '@posthog/ai'import { PostHog } from 'posthog-node'@@ -570,7 +578,7 @@ const posthog = new PostHog('${projectApiKey}', {})const prompts = new Prompts({ posthog })-const result = await prompts.get(${JSON.stringify(promptName)}, { ${labelArg}fallback: 'You are a helpful assistant.' })${compileLines}+const result = await prompts.get(${JSON.stringify(promptName)}, { ${labelArg}fallback: 'You are a helpful assistant.' })${compileLines}${configLines}// result.name / result.version -> send as $ai_prompt_name / $ai_prompt_version on your LLM events`}@@ -588,6 +596,8 @@ export function PromptCodeSnippets({ prompt }: { prompt: LLMPrompt }): JSX.Elemeconst snippetLabel = labelsEnabled? (promptLabels.find((label) => label.name === 'production') ?? promptLabels[0])?.name: undefined+ // Same principle for config: only show the usage line once this prompt has one.+ const showConfig = !!featureFlags[FEATURE_FLAGS.LLM_PROMPT_CONFIG] && prompt.config != nullreturn (<div className="mb-6" data-attr="llma-prompt-code-snippets">@@ -620,7 +630,14 @@ export function PromptCodeSnippets({ prompt }: { prompt: LLMPrompt }): JSX.Elemelabel: 'Python',content: (<CodeSnippet language={Language.Python}>- {buildPythonSnippet(prompt.name, host, projectApiKey, variables, snippetLabel)}+ {buildPythonSnippet(+ prompt.name,+ host,+ projectApiKey,+ variables,+ snippetLabel,+ showConfig+ )}</CodeSnippet>),},@@ -629,7 +646,14 @@ export function PromptCodeSnippets({ prompt }: { prompt: LLMPrompt }): JSX.Elemelabel: 'Node.js',content: (<CodeSnippet language={Language.JavaScript}>- {buildNodeSnippet(prompt.name, host, projectApiKey, variables, snippetLabel)}+ {buildNodeSnippet(+ prompt.name,+ host,+ projectApiKey,+ variables,+ snippetLabel,+ showConfig+ )}</CodeSnippet>),},