API-command for retrieving the context_length

I’m using the openai python module. How can I retrieve the context_length for a model (or a list for all models)?

gpt-3.5-turbo suggested the following, which does not work:

models = openai.Model.list()
for model in models['data']:
    if model['id'] == 'davinci':
        config = model['model_configuration']
        context_length = config['max_tokens']

print(f"The context length for the davinci model is {context_length} tokens.")
KeyError                                  Traceback (most recent call last)
Cell In [21], line 4
      2 for model in models['data']:
      3     if model['id'] == 'davinci':
----> 4         config = model['model_details']['config']
      5         context_length = config['max_tokens']
      6         break

KeyError: 'model_details'

Hi @david.nordfors

Please refer to the models endpoint docs when making call to the API.

Based on the docs, currently there doesn’t seem to be a way for retrieving model context length via API.

However I like the idea of being able to programmatically retrieve the context length via API.