Please create the Python code necessary to implement a Twitch.tv GPT3 chatbot that that responds to users prompts in chat only when addressed directly by name to keep costs down. The chatbot should be able also to retain and learn from a history of the comments in chat even when it is not actively involved in any interaction taking place.
Here is a simple implementation of the Twitch.tv chatbot using the GPT-3 API:

```python
import openai

# Initialize API key for OpenAI
openai.api_key = "YOUR_API_KEY"

# Define the chatbot's name
BOT_NAME = "ChatGPT"

# Retain a history of comments in a list
chat_history = []

# Continuously listen to the chat stream and respond when addressed directly by name
while True:
    # Get the latest comment in the chat
    comment = get_latest_comment()

    # Add the comment to the chat history
    chat_history.append(comment)

    # Check if the comment mentions the chatbot's name
    if BOT_NAME in comment:
        # Use GPT-3 to generate a response
        response = openai.Completion.create(
            engine="text-davinci-002",
            prompt=f"{BOT_NAME}, {comment}",
            max_tokens=1024,
            n=1,
            stop=None,
            temperature=0.5,
        ).choices[0].text

        # Post the response in the chat
        post_message(response)


```

Note: The code assumes the existence of two functions, `get_latest_comment()` and `post_message(message)`, which are responsible for fetching the latest comment from the Twitch chat and posting a message to the Twitch chat, respectively. You will need to implement these functions based on the specific Twitch API you are using.