Use prompts directly via SDK without the AI Gateway
When building LLM applications, you sometimes need direct control over prompt compilation without routing through the AI Gateway. The SDK provides an alternative integration method that allows you to pull and compile prompts directly in your application.
The SDK provides types for both integration methods when using the OpenAI SDK:
Type
Description
Use Case
HeliconeChatCreateParams
Standard chat completions with prompts
Non-streaming requests
HeliconeChatCreateParamsStreaming
Streaming chat completions with prompts
Streaming requests
Both types extend the OpenAI SDK’s chat completion parameters and add:
prompt_id - Your saved prompt identifier
environment - Optional environment to target (e.g., “production”, “staging”)
version_id - Optional specific version (defaults to production version)
inputs - Variable values
Important: These types make messages optional because Helicone prompts are expected to contain the required message structure. If your prompt template is empty or doesn’t include messages, you’ll need to provide them at runtime.For direct SDK integration:
Copy
Ask AI
import { HeliconePromptManager } from '@helicone/helpers';const promptManager = new HeliconePromptManager({ apiKey: "your-helicone-api-key"});
The SDK provides types that extend OpenAI’s official types:
Type
Description
Use Case
HeliconeChatParams
Chat completion parameters with prompt support (includes environment)
All prompt requests
PromptCompilationResult
Result with body and validation errors
Error handling
The HeliconeChatParams type includes all OpenAI parameters plus:
prompt_id - Your saved prompt identifier
environment - Optional environment to target (e.g., “production”, “staging”)
version_id - Optional specific version (defaults to production version)
inputs - Variable values for template substitution
Important: Similar to TypeScript, messages becomes optional when using prompts since your saved prompt template should contain the necessary message structure.The main class for direct SDK integration:
Copy
Ask AI
from helicone_helpers import HeliconePromptManagerprompt_manager = HeliconePromptManager( api_key="your-helicone-api-key")
import OpenAI from 'openai';import { HeliconePromptManager } from '@helicone/helpers';const openai = new OpenAI({ baseURL: "https://ai-gateway.helicone.ai", apiKey: process.env.HELICONE_API_KEY,});const promptManager = new HeliconePromptManager({ apiKey: "your-helicone-api-key"});async function generateWithPrompt() { // Get compiled prompt with variable substitution const { body, errors } = await promptManager.getPromptBody({ prompt_id: "abc123", model: "gpt-4o-mini", inputs: { customer_name: "Alice Johnson", product: "AI Gateway" } }); // Check for validation errors if (errors.length > 0) { console.warn("Validation errors:", errors); } // Use compiled prompt with OpenAI SDK const response = await openai.chat.completions.create(body); console.log(response.choices[0].message.content);}
Copy
Ask AI
import openaiimport osfrom helicone_helpers import HeliconePromptManagerclient = openai.OpenAI( base_url="https://ai-gateway.helicone.ai", api_key=os.environ.get("HELICONE_API_KEY"))prompt_manager = HeliconePromptManager( api_key="your-helicone-api-key")def generate_with_prompt(): # Get compiled prompt with variable substitution result = prompt_manager.get_prompt_body({ "prompt_id": "abc123", "model": "gpt-4o-mini", "inputs": { "customer_name": "Alice Johnson", "product": "AI Gateway" } }) # Check for validation errors if result["errors"]: print("Validation errors:", result["errors"]) # Use compiled prompt with OpenAI SDK response = client.chat.completions.create(**result["body"]) print(response.choices[0].message.content)
Both approaches are fully compatible with all OpenAI SDK features including function calling, response formats, and advanced parameters. The HeliconePromptManager, while not providing input traces, will provide validation error handling.
Prompt partials allow you to reference messages from other prompts using the syntax {{hcp:prompt_id:index:environment}}. This enables code reuse across your prompt library.
Prompt partials use the format {{hcp:prompt_id:index:environment}}:
prompt_id - The 6-character alphanumeric identifier of the prompt to reference
index - The message index (0-based) to extract from that prompt
environment - Optional environment identifier (defaults to production)
Examples:
Copy
Ask AI
{{hcp:abc123:0}} // Message 0 from prompt abc123 (production){{hcp:abc123:1:staging}} // Message 1 from prompt abc123 (staging){{hcp:xyz789:2:development}} // Message 2 from prompt xyz789 (development)
If your prompts don’t contain any prompt partials (no {{hcp:...}} tags), you don’t need to worry about this section. The SDK will work normally without any special handling.
When using the SDK directly, each prompt partial requires a separate API call to fetch the referenced prompt. For prompts with many partials, consider using the AI Gateway instead for better performance and automatic caching.