DEV Community

Ahmed Rakan
Ahmed Rakan

Posted on

Stop Burning Cash on AI Generation in Development

I’m building a startup with a very complex AI workflow — combining text generation and image generation.

Here’s the catch:

  • Text generation costs are negligible for one man team like mine, but not so much for larger teams.
  • Image generation? Not so much. Using DALL·E 3 costs about \$0.04 per image (standard size).

At first, I underestimated how fast this adds up. But when you consider:

  • Team size,
  • Frequency of commits,
  • Automated tests or integration runs,

…it’s easy to rack up serious costs just from developers iterating on code.

The Problem

If every commit triggers image generation in dev/test environments, you’re basically paying per debug cycle. That’s unsustainable.

The Solution: Provider Pattern

Instead of letting dev environments hit expensive APIs, use a provider pattern with a mock fallback.

  • In staging/production, use the real DALL·E provider.
  • In development, serve mock images (placeholders) to keep costs at zero.

Here’s a simplified example:

import { DallEAPIWrapper } from '@langchain/openai';
import { ENV } from '../getEnv';

type ImageServiceOptions = {
  description: string;
  size?: '256x256' | '512x512' | '1024x1024';
  quality?: 'standard' | 'hd';
  n?: number;
};

const defaultParams = {
  size: '1024x1024' as const,
  quality: 'standard' as const,
  n: 1,
  model: 'dall-e-3',
};

function buildFoodPrompt(description: string): string {
  return `
  You are professional photographer generate image for the following

  User description:
${description}
  `.trim();
}

// --- Provider Interface ---
interface ImageProvider {
  generate(options: ImageServiceOptions): Promise<string | null>;
}

// --- Providers ---
class DalleProvider implements ImageProvider {
  async generate({ description, size, quality, n }: ImageServiceOptions) {
    const dalle = new DallEAPIWrapper({
      size: size ?? defaultParams.size,
      quality: quality ?? defaultParams.quality,
      n: n ?? defaultParams.n,
      model: defaultParams.model,
      style: 'natural',
    });

    return (await dalle.invoke(buildFoodPrompt(description))) ?? null;
  }
}

class MockProvider implements ImageProvider {
  async generate({ description }: ImageServiceOptions) {
    // Could log description to mimic "generation" in dev
    console.log('Mock generation for:', description);
    return 'https://placehold.co/600x400.png';
  }
}

// --- Provider Factory ---
function getProvider(): ImageProvider {
  return ENV === 'dev' ? new MockProvider() : new DalleProvider();
}

// --- Service function using Provider Pattern ---
export async function generateMenuItemImage(
  options: ImageServiceOptions
): Promise<string | null> {
  const provider = getProvider();
  return provider.generate(options);
}

Enter fullscreen mode Exit fullscreen mode

Key Takeaway

  • Run full AI generation tests only in staging.
  • Use mock providers in dev.
  • This keeps your workflow lean, reliable, and cost-efficient.

If you’re building AI-powered apps, remember: not every debug session should cost \$0.04.

Top comments (0)