Paygent integration#386
Open
nihal0514 wants to merge 2 commits into
Open
Conversation
stefandsl
added a commit
to stefandsl/dograh
that referenced
this pull request
Jun 2, 2026
Upstream dograh-hq#386 (commit fdf137f). Adds opt-in Paygent billing/usage tracking: a fire-and-forget pipeline metrics aggregator + agent backfill/ensure. Fully gated on PAYGENT_API_KEY — when unset, the aggregator is never built and all sync calls are no-ops, so behavior is unchanged without config. Fork-specific adjustments (deliberately deviating from the PR): - KEPT the fork's pipecat submodule pointer (228324a); did NOT take the PR's bump to c6f3906, which would have reverted fork pipecat work. Verified all pipecat symbols paygent.py imports resolve against 228324a. - DROPPED the repo-level docker-compose.override.yaml the PR adds — irrelevant to this deployment (build/secrets come from the operator override at /root). Verified: full app imports cleanly, paygent modules load, workflow creation unaffected without PAYGENT_API_KEY. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
stefandsl
added a commit
to stefandsl/dograh
that referenced
this pull request
Jun 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request: Paygent Integration
This pull request integrates Paygent into the Dograh backend to enable real-time cost-tracking and automatic agent/customer registration for both standard and speech-to-speech (Realtime) pipeline runs.
🚀 Overview of Changes
We have added robust, non-blocking telemetry and synchronization with the Paygent API. All HTTP interactions are executed asynchronously on background threads using a
ThreadPoolExecutorto guarantee that they never block the FastAPI event loop or the live audio pipeline. All external API interactions are fully exception-guarded to ensure any network or Paygent failures never degrade the voice call experience.1. Pipecat Submodule Enhancements (
pipecat/)To support full multimodal speech-to-speech cost tracking (such as OpenAI Realtime and Gemini Live), we extended the core Pipecat metrics layer to preserve and pass-through the underlying rich usage metadata:
src/pipecat/metrics/metrics.py: Added an optionalraw_usage_metadatafield to the standardLLMTokenUsageschema.src/pipecat/services/google/gemini_live/llm.py: Extracted the richusage_metadatapayload (containing detailed modality splits for prompt, response, tool-use, and cache) and attached it toLLMTokenUsage.src/pipecat/services/openai/realtime/llm.py: Captured and attached the fullusagemodel dump (including audio, text, cached token breakdowns) toLLMTokenUsage.2. Core Telemetry Aggregator (
api/services/pipecat/paygent.py[NEW])Created
PaygentPipelineMetricsAggregator, a custom PipecatFrameProcessorthat intercepts metric frames and forwards events:/api/v1/voice/llm, TTS characters via/api/v1/voice/tts(with intelligent fallback estimation based on completion tokens for providers like Deepgram WS that don't emit metrics), STT approximated from call duration via/api/v1/voice/stt, and finalizes with/api/v1/voice/indicator./api/v1/voice/speech-to-speech. Includes special-case minute-based billing for providers like Grok and Ultravox.localhost->host.docker.internalinside Docker environments), and retry logic for network timeouts or 5xx/429 status codes.3. Asynchronous Agent Registration (
api/services/pipecat/paygent_agent_sync.py[NEW])Introduced a module to register Dograh workflows as Paygent voice agents:
ensure_agent_async(workflow_id, workflow_name): Idempotently registers/updates a voice agent via/api/v2/agents/ensure. Safely runs as an asynchronous background task on the server's thread pool.run_backfill_if_requested()to scan and sync existing active workflows to Paygent in a paginated, non-blocking manner at server startup ifPAYGENT_BACKFILL=trueis set.4. Integration Points & Hookups
api/app.py: Triggers the startup backfill during the lifespan startup sequence.api/routes/workflow.py: Hooks into the workflow creation flows (create_workflow,create_workflow_from_template) to register new workflows as voice agents in the background.api/services/pipecat/pipeline_builder.py: Integrated the newpaygent_aggregatoras an optional processor in pipeline structures.api/services/pipecat/run_pipeline.py: Sourced all Paygent environment configurations, derived end-customer identifiers dynamically (telephony outbound maps to called number, inbound maps to caller number, and web calls useweb-call), instantiatedPaygentPipelineMetricsAggregator, and injected it into the running call pipeline.docker-compose.override.yaml[NEW]: Added a local development override to set context and load.envconfigurations automatically for theapiservice.🛠️ Configuration & Environment Variables
The integration is fully optional and nil-safe. If
PAYGENT_API_KEYis not present in the environment, all modules gracefully become silent no-ops.🧪 Verification & Testing
PAYGENT_API_KEYis absent, standard and realtime call sessions spin up with zero impact or log noise.web-callfor WebRTC sessions.