Back to Blog

Python + Claude API: Your First AI App in Neovim | Episode 1

Celest KimCelest Kim

Video: Python + Claude API: Your First AI App in Neovim | Episode 1 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Python + Claude API: Your First AI App in Neovim

Learn how to build your first AI-powered app using Python and the Claude API, all within the Neovim editor. This guide walks you through installing the Anthropic SDK, securely managing your API key with a .env file, and writing a simple script that sends a message to Claude and receives a response.

Code

# Install dependencies in your terminal:
# pip install anthropic python-dotenv

import os
from dotenv import load_dotenv
import anthropic

# Load environment variables from .env file
load_dotenv()

# Retrieve your Anthropic API key securely
API_KEY = os.getenv("ANTHROPIC_API_KEY")

# Create an Anthropic client
client = anthropic.Client(API_KEY)

# Define a prompt for Claude
prompt = "Hello Claude, can you briefly explain what the Claude API is?"

# Send a message to Claude and get a response
response = client.completions.create(
  model="claude-v1",          # Specify the model
  prompt=anthropic.HUMAN_PROMPT + prompt + anthropic.AI_PROMPT,
  max_tokens_to_sample=200,   # Limit the response length
  stop_sequences=[anthropic.HUMAN_PROMPT]
)

print("Claude's response:")
print(response.completion)

Key Points

  • Install the Anthropic SDK and python-dotenv to interact with the Claude API and manage environment variables.
  • Store your API key in a .env file to keep it secure and load it in your Python script using load_dotenv().
  • Create an Anthropic client with your API key to send requests to Claude.
  • Use client.completions.create() with model, prompt, max_tokens_to_sample, and stop_sequences to generate AI completions.
  • Run your script in Neovim to see Claude's AI-generated response directly in your terminal.