<type>documentation</type>
<domain>ai</domain>
<id>context</id>
<title>Managing Cost</title>
<date>$Date: 2006-05-24 22:53:07 -0400 (Wed, 24 May 2006) $</date>
<version>$Revision: 13098 $</version>
<product>pro+</product>

When you use Wing's AI assisted development features, it sends requests to your selected
AI provider, who may charge your account according to the size of the request and response.

You can see the cost of your last request, and the cost of all requests made so far, at the bottom
of the ``AI Coder`` and ``AI Chat`` tools. These totals are stored on a per-project basis and can be
reset to zero with ``Reset Costs`` in the ``AI`` tool's ``Options`` menu.

Note that you may need to configure the currency and token costs set in your AI provider
configuration to match what your account is actually being charged.

The costs displayed by Wing are estimates and may vary from the actual charges made, which
you can track from your AI provider account login.

**Controlling Costs**

The most important way in which you can control costs is to limit the amount of context
that you include with requests, as described in `AI Request Context <ref:ai/context>`__.
In short, only include extra context when it is necessary.

Note that the size of the ``Target`` in AI refactoring and ``Focus`` in AI chat also add
to costs, so it's best to use the smallest target or focus that still allows the AI to
provide accurate responses.

Learning what sorts of requests are likely to return useful results is also important. See
`Improving Results Obtained from AI Assisted Development <ref:ai/tips>`__ for details.

**OpenAI Data Storage Costs**

If and only if you are using OpenAI as your AI provider, you may also incur storage costs for files
left in your AI provider account. The costs reported by Wing do not include storage costs for any
uploaded files left in your OpenAI account.

This only occurs if you have used the ``AI Chat`` tool and Wing fails to remove all uploaded files
after they are no longer needed, for example as a result of a network outtage. In that case, files
may be left in your account until Wing retries removing them, and you may be charged by OpenAI for
their storage during that time. 

See the OpenAI Account Resources in the documentation for `AI Chat <ref:ai/chat>`__ to learn how to
manually remove data Wing has created in your OpenAI account.

**Rate Limits**

Your AI provider may impose rate limits that depend on which AI model you are using, how much you
have spent with them so far, and how old your account is. These are often expressed in token counts
per minute or day and not in terms of cost or raw request size.

The approximate maximum number of tokens consumed by a request can be computed by adding
the configured maximum context size to the size of the returned code, dividing this by 5,
and adding 500. Thus a request with 150KB of context that returns 10KB of code will
consume about (150000 + 10000) / 5 + 500 = 32500 tokens.

The number of tokens that might be consumed should be compared to your AI provider's rate limits to
determine the number of requests you will be able to make per minute and per day. If you run into
rate limits, you may be able to add more money to your AI provider account to solve the problem.
