| name | deepgram-performance-tuning |
| description | Optimize Deepgram API performance for faster transcription and lower latency.
Use when improving transcription speed, reducing latency,
or optimizing audio processing pipelines.
Trigger with phrases like "deepgram performance", "speed up deepgram",
"optimize transcription", "deepgram latency", "deepgram faster".
|
| allowed-tools | Read, Write, Edit, Bash(gh:*), Bash(curl:*) |
| version | 1.0.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
Deepgram Performance Tuning
Overview
Optimize Deepgram integration performance through audio preprocessing, connection management, and configuration tuning.
Prerequisites
- Working Deepgram integration
- Performance monitoring in place
- Audio processing capabilities
- Baseline metrics established
Performance Factors
| Factor | Impact | Optimization |
|---|
| Audio Format | High | Use optimal encoding |
| Sample Rate | Medium | Match model requirements |
| File Size | High | Stream large files |
| Model Choice | High | Balance accuracy vs speed |
| Network Latency | Medium | Use closest region |
| Concurrency | Medium | Manage connections |
Instructions
Step 1: Optimize Audio Format
Preprocess audio for optimal transcription.
Step 2: Configure Connection Pooling
Reuse connections for better throughput.
Step 3: Tune API Parameters
Select appropriate model and features.
Step 4: Implement Streaming
Use streaming for real-time and large files.
Examples
Audio Preprocessing
import ffmpeg from 'fluent-ffmpeg';
import { Readable } from 'stream';
interface OptimizedAudio {
buffer: Buffer;
mimetype: string;
sampleRate: number;
channels: number;
duration: number;
}
export async function optimizeAudio(inputPath: string): Promise<OptimizedAudio> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
ffmpeg(inputPath)
.audioCodec('pcm_s16le')
.audioChannels(1)
.audioFrequency(16000)
.format('wav')
.on(, reject)
.(, {
buffer = .(chunks);
({
buffer,
: ,
: ,
: ,
: buffer. / ( * ),
});
})
.()
.(, chunks.(chunk));
});
}
(): <> {
( {
: [] = [];
readable = ();
readable.(audioBuffer);
readable.();
(readable)
.(inputFormat)
.()
.()
.()
.()
.(, reject)
.(, (.(chunks)))
.()
.(, chunks.(chunk));
});
}
Connection Pooling
import { createClient, DeepgramClient } from '@deepgram/sdk';
interface PoolConfig {
minSize: number;
maxSize: number;
acquireTimeout: number;
idleTimeout: number;
}
class DeepgramConnectionPool {
private pool: DeepgramClient[] = [];
private inUse: Set<DeepgramClient> = new Set();
private waiting: Array<(client: DeepgramClient) => void> = [];
private config: PoolConfig;
private apiKey: string;
constructor(apiKey: string, config: Partial<PoolConfig> = {}) {
this.apiKey = apiKey;
this.config = {
minSize: config.minSize ?? ,
: config. ?? ,
: config. ?? ,
: config. ?? ,
};
( i = ; i < ..; i++) {
..((.));
}
}
(): <> {
(.. > ) {
client = ..()!;
..(client);
client;
}
(.. < ..) {
client = (.);
..(client);
client;
}
( {
timeout = ( {
index = ..(resolve);
(index > -) ..(index, );
( ());
}, ..);
..( {
(timeout);
(client);
});
});
}
(: ): {
..(client);
(.. > ) {
waiter = ..()!;
..(client);
(client);
} {
..(client);
}
}
execute<T>(: <T>): <T> {
client = .();
{
(client);
} {
.(client);
}
}
() {
{
: ..,
: ..,
: ..,
};
}
}
pool = (process..!);
Streaming for Large Files
import { createClient } from '@deepgram/sdk';
import { createReadStream, statSync } from 'fs';
interface StreamingOptions {
chunkSize: number;
model: string;
}
export async function streamLargeFile(
filePath: string,
options: Partial<StreamingOptions> = {}
): Promise<string> {
const { chunkSize = 1024 * 1024, model = 'nova-2' } = options;
const client = createClient(process.env.DEEPGRAM_API_KEY!);
const fileSize = statSync(filePath).size;
const transcripts: string[] = [];
const connection = client.listen.live({
model,
smart_format: true,
punctuate: true,
});
return new Promise(() => {
connection.(, {
stream = (filePath, { : chunkSize });
stream.(, {
connection.(chunk);
});
stream.(, {
connection.();
});
stream.(, reject);
});
connection.(, {
(data.) {
transcripts.(data..[].);
}
});
connection.(, {
(transcripts.());
});
connection.(, reject);
});
}
Model Selection for Speed
interface ModelConfig {
name: string;
accuracy: 'high' | 'medium' | 'low';
speed: 'fast' | 'medium' | 'slow';
costPerMinute: number;
}
const models: Record<string, ModelConfig> = {
'nova-2': {
name: 'Nova-2',
accuracy: 'high',
speed: 'fast',
costPerMinute: 0.0043,
},
'nova': {
name: 'Nova',
accuracy: 'high',
speed: 'fast',
costPerMinute: 0.0043,
},
'enhanced': {
name: 'Enhanced',
accuracy: 'medium',
speed: 'fast',
costPerMinute: 0.0145,
},
'base': {
name: 'Base',
accuracy: 'low',
speed: 'fast',
: ,
},
};
(): {
{ prioritize, minAccuracy = } = requirements;
accuracyOrder = [, , ];
minAccuracyIndex = accuracyOrder.(minAccuracy);
eligible = .(models).(
accuracyOrder.(config.) <= minAccuracyIndex
);
(prioritize === ) {
eligible.(
accuracyOrder.(config.) < accuracyOrder.(models[best].)
? name : best
, eligible[][]);
}
(prioritize === ) {
eligible.(
config. < models[best]. ? name : best
, eligible[][]);
}
;
}
Parallel Processing
import { pool } from './connection-pool';
import pLimit from 'p-limit';
interface TranscriptionResult {
file: string;
transcript: string;
duration: number;
}
export async function transcribeMultiple(
audioUrls: string[],
concurrency = 5
): Promise<TranscriptionResult[]> {
const limit = pLimit(concurrency);
const startTime = Date.now();
const results = await Promise.all(
audioUrls.map((url, index) =>
limit(async () => {
const itemStart = Date.now();
const result = await pool.execute(async (client) => {
const { result, error } = await client.listen.prerecorded.(
{ url },
{ : , : }
);
(error) error;
result;
});
{
: url,
: result..[].[].,
: .() - itemStart,
};
})
)
);
.();
.();
results;
}
Caching Results
import { createHash } from 'crypto';
import { redis } from './redis';
interface CacheOptions {
ttl: number;
}
export class TranscriptionCache {
private ttl: number;
constructor(options: Partial<CacheOptions> = {}) {
this.ttl = options.ttl ?? 3600;
}
private getCacheKey(audioUrl: string, options: Record<string, unknown>): string {
const hash = createHash('sha256')
.update(JSON.stringify({ audioUrl, options }))
.digest('hex');
return `transcription:${hash}`;
}
async get(
audioUrl: string,
: <, >
): < | > {
key = .(audioUrl, options);
redis.(key);
}
(
: ,
: <, >,
:
): <> {
key = .(audioUrl, options);
redis.(key, ., transcript);
}
(
: <>,
: ,
: <, >
): <{ : ; : }> {
cached = .(audioUrl, options);
(cached) {
{ : cached, : };
}
transcript = ();
.(audioUrl, options, transcript);
{ transcript, : };
}
}
Performance Metrics
import { Histogram, Counter, Gauge } from 'prom-client';
export const transcriptionLatency = new Histogram({
name: 'deepgram_transcription_latency_seconds',
help: 'Latency of transcription requests',
labelNames: ['model', 'status'],
buckets: [0.5, 1, 2, 5, 10, 30, 60],
});
export const audioDuration = new Histogram({
name: 'deepgram_audio_duration_seconds',
help: 'Duration of audio files processed',
buckets: [10, 30, 60, 120, 300, 600, 1800],
});
export const processingRatio = new Gauge({
name: 'deepgram_processing_ratio',
help: 'Ratio of processing time to audio duration',
labelNames: ['model'],
});
export () {
audioDuration.(audioDurationSec);
processingRatio.(model).(processingTimeSec / audioDurationSec);
}
Resources
Next Steps
Proceed to deepgram-cost-tuning for cost optimization.