Unexpectedly Large Token Count for Image Analysis Using GPT-4 API

I’m experiencing an issue with the GPT-4 API where a single image upload results in an unexpectedly high token count, leading to a “Request too large” error.


  • Flutter frontend sends images via multipart/form-data.
  static Future<dynamic> analyzeImages(List<File> images) async {
    var uri = Uri.parse('$baseUrl/analyze_images/');
    var request = http.MultipartRequest('POST', uri);

    for (File image in images) {
      var stream = http.ByteStream(image.openRead());
      var length = await image.length();
      var multipartFile = http.MultipartFile('images', stream, length, filename: image.path.split('/').last);

    var response = await request.send();
    if (response.statusCode == 200) {
      var responseData = await response.stream.bytesToString();
      return json.decode(responseData);
    } else {
      throw Exception('Failed to analyze images');
  • Django backend encodes images in base64 and sends them to the GPT-4 API.
def analyze_images(request):
    if request.method == 'POST':
        images = request.FILES.getlist('images')
        if images:
            responses = []
            headers = {
                'Authorization': f'Bearer sk-proj-',
                'Content-Type': 'application/json',

            for image in images:
                print(f"Processing image: {image.name}")
                image_content = image.read()
                print(f"Image size (bytes): {len(image_content)}")

                encoded_image = base64.b64encode(image_content).decode('utf-8')
                print(f"Encoded image size (chars): {len(encoded_image)}")

                json_data = {
                    "model": "gpt-4o",
                    "messages": [
                        {"role": "system", "content": "You are an AI that analyzes images."},
                        {"role": "user", "content": "Analyze this image:"},
                        {"role": "user", "content": f"data:image/jpeg;base64,{encoded_image}"}
                    "max_tokens": 500,
                    "temperature": 0.5

                json_data_str = str(json_data)
                print(f"Request JSON size (chars): {len(json_data_str)}")

                response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=json_data)

            return JsonResponse({'results': responses})
            return JsonResponse({'error': 'No images provided'}, status=400)
        return JsonResponse({'error': 'Invalid request method'}, status=400)


  • Image size: 1 MB
  • Encoded image size: 1.4 million chars
  • JSON request size: 1.4 million chars
  • Resulting in 354,654 tokens.

The image is normal-sized, and I followed the official documentation. Is there a known issue or a specific encoding method required to reduce the token count?


Processing image: example.jpg
Image size (bytes): 1063892
Encoded image size (chars): 1418524
Request JSON size (chars): 1418774

The weirdest thing is that when I do it by URL with the same image the tokens are 85 not 350.000, what I’m saying is that something must be wrong otherwise they wouldn’t put that you can upload images locally but only by URL.

Any guidance on resolving this issue would be appreciated.

You are sending the AI a string. You are not sending an image. The AI receives the data as AI language.

This is the proper way to send an image object:

        "role": "user",
        "content": [
            {"type": "text", "text": 'How many bananas?'},
                "type": "image_url",
                "image_url": {"url": f"data:image/x-png;base64,{base64_image}", "detail": "low"},

Thank you for your prompt response! Your suggestion to use an image object instead of a string solved the issue. By sending the image as an image_url object within the JSON, the token count was significantly reduced, and the API call is now working correctly. I appreciate the guidance and the quick solution.

Hi @christopheracousin and @_j, I’m having a similar setup but still experiencing the issue. Could you kindly have a look

Thanks a lot!