Lab 1 bug: maximum context length exceeded

There seems to be a bug in Lab 1. Can anyone from Deep Learning fix Lab 1?

When executing the second last step of the lab

result = run_agent('Show me the code for graph of sales by store in Nov 2021, and tell me what trends you see.')

I get the error

Running agent with messages: Show me the code for graph of sales by store in Nov 2021, and tell me what trends you see.
Making router call to OpenAI
messages:  [{'role': 'user', 'content': 'Show me the code for graph of sales by store in Nov 2021, and tell me what trends you see.'}, {'role': 'system', 'content': '\nYou are a helpful assistant that can answer questions about the Store Sales Price Elasticity Promotions dataset.\n'}]
Received response with tool calls: True
Processing tool calls
Making router call to OpenAI
messages:  [{'role': 'user', 'content': 'Show me the code for graph of sales by store in Nov 2021, and tell me what trends you see.'}, {'role': 'system', 'content': '\nYou are a helpful assistant that can answer questions about the Store Sales Price Elasticity Promotions dataset.\n'}, ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_Nzr8hOoUxpCTcIDG62kDXSQ0', function=Function(arguments='{"prompt":"Show me sales data for all stores in November 2021."}', name='lookup_sales_data'), type='function')], annotations=[]), {'role': 'tool', 'content': '       Store_Number  SKU_Coded  Product_Class_Code  Sold_Date  Qty_Sold  Total_Sale_Value  On_Promo\n0              1320    6172800               22875 2021-11-02         3         56.849998         0\n1              2310    6172800             <snip>941036 tokens worth of data</snip>', 'tool_call_id': 'call_Nzr8hOoUxpCTcIDG62kDXSQ0'}]
---------------------------------------------------------------------------
BadRequestError                           Traceback (most recent call last)
Cell In[29], line 1
----> 1 result = run_agent('Show me the code for graph of sales by store in Nov 2021, and tell me what trends you see.')

Cell In[28], line 17, in run_agent(messages)
     15 print("Making router call to OpenAI")
     16 print("messages: ", messages)
---> 17 response = client.chat.completions.create(
     18     model=MODEL,
     19     messages=messages,
     20     tools=tools,
     21 )
     22 messages.append(response.choices[0].message)
     23 tool_calls = response.choices[0].message.tool_calls

File /usr/local/lib/python3.11/site-packages/openai/_utils/_utils.py:275, in required_args.<locals>.inner.<locals>.wrapper(*args, **kwargs)
    273             msg = f"Missing required argument: {quote(missing[0])}"
    274     raise TypeError(msg)
--> 275 return func(*args, **kwargs)

File /usr/local/lib/python3.11/site-packages/openai/resources/chat/completions.py:829, in Completions.create(self, messages, model, audio, frequency_penalty, function_call, functions, logit_bias, logprobs, max_completion_tokens, max_tokens, metadata, modalities, n, parallel_tool_calls, prediction, presence_penalty, response_format, seed, service_tier, stop, store, stream, stream_options, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)
    788 @required_args(["messages", "model"], ["messages", "model", "stream"])
    789 def create(
    790     self,
   (...)
    826     timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
    827 ) -> ChatCompletion | Stream[ChatCompletionChunk]:
    828     validate_response_format(response_format)
--> 829     return self._post(
    830         "/chat/completions",
    831         body=maybe_transform(
    832             {
    833                 "messages": messages,
    834                 "model": model,
    835                 "audio": audio,
    836                 "frequency_penalty": frequency_penalty,
    837                 "function_call": function_call,
    838                 "functions": functions,
    839                 "logit_bias": logit_bias,
    840                 "logprobs": logprobs,
    841                 "max_completion_tokens": max_completion_tokens,
    842                 "max_tokens": max_tokens,
    843                 "metadata": metadata,
    844                 "modalities": modalities,
    845                 "n": n,
    846                 "parallel_tool_calls": parallel_tool_calls,
    847                 "prediction": prediction,
    848                 "presence_penalty": presence_penalty,
    849                 "response_format": response_format,
    850                 "seed": seed,
    851                 "service_tier": service_tier,
    852                 "stop": stop,
    853                 "store": store,
    854                 "stream": stream,
    855                 "stream_options": stream_options,
    856                 "temperature": temperature,
    857                 "tool_choice": tool_choice,
    858                 "tools": tools,
    859                 "top_logprobs": top_logprobs,
    860                 "top_p": top_p,
    861                 "user": user,
    862             },
    863             completion_create_params.CompletionCreateParams,
    864         ),
    865         options=make_request_options(
    866             extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
    867         ),
    868         cast_to=ChatCompletion,
    869         stream=stream or False,
    870         stream_cls=Stream[ChatCompletionChunk],
    871     )

File /usr/local/lib/python3.11/site-packages/openai/_base_client.py:1278, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls)
   1264 def post(
   1265     self,
   1266     path: str,
   (...)
   1273     stream_cls: type[_StreamT] | None = None,
   1274 ) -> ResponseT | _StreamT:
   1275     opts = FinalRequestOptions.construct(
   1276         method="post", url=path, json_data=body, files=to_httpx_files(files), **options
   1277     )
-> 1278     return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))

File /usr/local/lib/python3.11/site-packages/openai/_base_client.py:955, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)
    952 else:
    953     retries_taken = 0
--> 955 return self._request(
    956     cast_to=cast_to,
    957     options=options,
    958     stream=stream,
    959     stream_cls=stream_cls,
    960     retries_taken=retries_taken,
    961 )

File /usr/local/lib/python3.11/site-packages/openai/_base_client.py:1059, in SyncAPIClient._request(self, cast_to, options, retries_taken, stream, stream_cls)
   1056         err.response.read()
   1058     log.debug("Re-raising status error")
-> 1059     raise self._make_status_error_from_response(err.response) from None
   1061 return self._process_response(
   1062     cast_to=cast_to,
   1063     options=options,
   (...)
   1067     retries_taken=retries_taken,
   1068 )

BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 941177 tokens (941036 in the messages, 141 in the functions). Please reduce the length of the messages or functions.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}

I added an extra print("messages: ", messages) in run_agent like so

    while True:
        print("Making router call to OpenAI")
        print("messages: ", messages)
        response = client.chat.completions.create(
            model=MODEL,
            messages=messages,
            tools=tools,
        )
...

So you see the massive number of tokens sent to the model in the second messages: in the error above.

Getting the same issue