How magnivorg/prompt-layer-library works
Project type and purpose
• This is a Python client-library and middleware for PromptLayer’s REST API. It sits between your Python code and OpenAI’s SDK, intercepts and records every OpenAI call, and then forwards the original request to OpenAI while sending metadata to PromptLayer’s backend for tracking and analytics.
• The README confirms it is not a full-stack app but a wrapper/SDK that:
• Exposes the same OpenAI interface
• Logs every request, response, timings, tags
• Calls two external services: OpenAI API and PromptLayer’s own REST endpoints
File structure
• Top-level files:
• pyproject.toml, Makefile, .devcontainer, GitHub workflows → build/test/deploy
• promptlayer/ → core package
• tests/ → fixtures, unit/integration tests
• Within promptlayer/:
• promptlayer.py & promptlayer_base.py → core client class definitions
• promptlayer_mixins.py → methods to extend the client dynamically
• span_exporter.py → tracing instrumentation
• streaming/ → streaming API support (blueprint_builder, handlers, processor)
• track/ → logic for preparing and sending tracking payloads
• types/ → data models (PromptTemplate, RequestLog, etc.)
• templates.py, utils.py → helper functions
• Presence of config files (.pre-commit, GitHub actions) indicates CI/CD integration but not part of runtime architecture.
Key architecture insights from the README
• The client:
• Instantiates PromptLayer(api_key) → overrides openai client
• When user calls openai.Completion.create(..., pl_tags=[…]), the wrapper:
– Records start time, end time, request/response details
– Immediately forwards the operation to the real OpenAI API locally
– Posts a JSON log to https://api.promptlayer.com/track-request
• No local database or server. All persistence lives on PromptLayer’s cloud service.
Main components & their relationships
a. User Application
• Your Python code that imports promptlayer.PromptLayer and calls openai.*
b. PromptLayer Python SDK
• Core client (promptlayer.PromptLayer)
• Mixins (metrics, grouping, streaming support)
• Track module (builds track-request payload; calls HTTP client)
• Templates/types for serializing data
c. OpenAI Python SDK
• Underneath the wrapper; actual API calls go here
d. External Services
• OpenAI REST API endpoints (text completion, chat, embeddings, streaming)
• PromptLayer REST API (track-request, templates, tags)
e. CI/CD & Tests (not runtime but part of development workflow)
Architectural patterns & design principles
• Decorator/Proxy Pattern: PromptLayer wraps the OpenAI client, intercepts calls, and delegates execution
• Single Responsibility: Separate modules for streaming, tracking, templating, utils
• Pluggable Mixins: Extend core client with additional capabilities (groups, streaming, span export)
• External Integration: Stateless library; state lives in external APIs