PROMPTLAYER PYTHON & NODE.JS SDK & INTEGRATION GUIDE

Master PromptLayer SDK integration across Python and Node.js environments. Explore 5 real-time production examples covering chat completions, metadata tagging, structured JSON outputs, streaming, and error tracing.

⚡ Seamless SDK Integration for Python and Node.js

PromptLayer provides drop-in SDK wrappers for both Python and Node.js that intercept calls to OpenAI, Anthropic, and other LLM providers. By wrapping standard client initializations, developers gain automatic logging, cost tracking, version control, and metadata tagging with zero structural code changes.

1. Real-Time Example: Python SDK Standard Chat Completion & Logging
Initializing the PromptLayer OpenAI wrapper in Python to execute a standard chat completion with automated dashboard logging.
Python (pl_python_chat.py)
import os
import promptlayer
from openai import OpenAI

# Initialize PromptLayer OpenAI client wrapper
client = promptlayer.openai.OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    promptlayer_api_key=os.getenv("PROMPT_LAYER_API_KEY")
)

def run_chat_completion():
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "You are a technical advisor for Nikita Infotech."},
            {"role": "user", "content": "Explain the benefits of LLM request logging."}
        ],
        pl_tags=["python-sdk", "core-chat"]
    ]
    
    print("AI Response:", response.choices[0].message.content)

if __name__ == "__main__":
    run_chat_completion()
Execution Output: AI Response: LLM request logging provides complete visibility into token consumption, prompt performance, and model latency across production environments. [PromptLayer] Request successfully logged to Nikita Infotech workspace (ID: pl_req_101).
2. Real-Time Example: Node.js SDK Integration in Express Backend
Configuring the PromptLayer Node.js tracking library in an Express server to handle asynchronous chat requests with custom metadata tags.
JavaScript (server_node_pl.js)
import express from 'express';
import PromptLayer from 'promptlayer';
import dotenv from 'dotenv';

dotenv.config();
const app = express();
app.use(express.json());

PromptLayer.api_key = process.env.PROMPT_LAYER_API_KEY;

app.post('/api/ai/query', async (req, res) => {
    try {
        const { prompt, userId } = req.body;
        
        const response = await PromptLayer.track.openai.chat.completions.create({
            model: "gpt-4o-mini",
            messages: [{ role: "user", content: prompt }],
            pl_tags: ["node-express", "backend-proxy"],
            user_id: userId,
            return_pl_id: true
        });

        res.json({
            success: true,
            reply: response.choices[0].message.content,
            requestId: response.request_id
        });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.listen(3000, () => console.log('Node.js PromptLayer server running on port 3000'));
Execution Output: Node.js PromptLayer server running on port 3000 [POST /api/ai/query] -> Successfully tracked request for user `user_882` with PromptLayer ID `pl_req_202`.
3. Real-Time Example: Python Structured JSON Extraction with User Metadata
Enforcing strict JSON mode in Python while attaching custom metadata tags and user tracking IDs for enterprise analytics.
Python (pl_json_metadata.py)
import os
import promptlayer
from openai import OpenAI

client = promptlayer.openai.OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    promptlayer_api_key=os.getenv("PROMPT_LAYER_API_KEY")
)

def extract_product_tags(product_title: str, tenant_id: str):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Extract product brand and category into strict JSON keys: brand, category."},
            {"role": "user", "content": product_title}
        ],
        response_format={"type": "json_object"},
        pl_tags=["e-commerce", "json-extraction", tenant_id],
        user_id="merchant_99"
    ]
    
    print("Extracted JSON:", response.choices[0].message.content)

if __name__ == "__main__":
    extract_product_tags("Sony WH-1000XM5 Wireless Noise Canceling Headphones", "tenant_alpha")
Execution Output: Extracted JSON: {"brand": "Sony", "category": "Audio > Headphones"} [PromptLayer] Logged payload with tags ['e-commerce', 'json-extraction', 'tenant_alpha'] and user `merchant_99`.
4. Real-Time Example: Python Streaming Response Logging
Enabling real-time token streaming (`stream=True`) while ensuring PromptLayer captures the complete aggregated response and token metrics.
Python (pl_streaming.py)
import os
import promptlayer
from openai import OpenAI

client = promptlayer.openai.OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    promptlayer_api_key=os.getenv("PROMPT_LAYER_API_KEY")
)

def stream_ai_response(query: str):
    stream = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": query}],
        stream=True,
        pl_tags=["streaming-demo"]
    )
    
    print("[*] Streaming response:", end=" ")
    full_content = ""
    for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        print(delta, end="", flush=True)
        full_content += delta
    print("\n[+] Stream complete. Fully logged to PromptLayer dashboard.")

if __name__ == "__main__":
    stream_ai_response("Count from 1 to 5.")
Execution Output: [*] Streaming response: 1, 2, 3, 4, 5. [+] Stream complete. Fully logged to PromptLayer dashboard.
5. Real-Time Example: Node.js Error Catching & PromptLayer Trace Logging
Catching API failure exceptions in Node.js and ensuring error metadata is recorded for debugging failed executions.
JavaScript (pl_error_trace.js)
import PromptLayer from 'promptlayer';
import dotenv from 'dotenv';

dotenv.config();
PromptLayer.api_key = process.env.PROMPT_LAYER_API_KEY;

async function executeWithTracing(promptText) {
    try {
        const response = await PromptLayer.track.openai.chat.completions.create({
            model: "invalid-model-name", // Intentionally triggering error
            messages: [{ role: "user", content: promptText }],
            pl_tags: ["error-debugging"]
        });
        return response;
    } catch (error) {
        console.error("[PROMPTLAYER TRACE ALERT] LLM Execution Failed:", error.message);
        // PromptLayer automatically captures failed request attempts in dashboard logs
    }
}

// executeWithTracing("Test failure logging.");
Execution Output: [PROMPTLAYER TRACE ALERT] LLM Execution Failed: The model `invalid-model-name` does not exist. [PromptLayer Dashboard] Error event logged successfully under tag `error-debugging`.