> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Setting Up Your Agents

> Guide to creating and running a Python ADK agent including virtual environment setup, installing google-adk, securing Gemini API key, scaffolding a starter agent, and running and iterating it

When does it go wrong?

You wrote a loop by hand and it crashed on a tool the agent didn't know about. Then it crashed again because an authority came back as a number instead of text.

```text theme={null}
$ python agent.py
model → cancel_order(order_id="1042")
KeyError: 'cancel_order' is not a registered tool
model → lookup_invoice(order_id=1042)
lookup_invoice → None
TypeError: expected str, got int - conversation ended
context: 131,900 tokens > 128,000 limit
agent: "Sorry, which order was that?"
ContextWindowExceeded - loop crashed
```

Now your manager wants to know why the agent promised a refund. You delete the whole file out of frustration. The idea was right, but you spent a week plumbing and wiring code when you should have been writing an agent.

This lesson rewinds and gets you started correctly. In about five minutes you'll have a real agent in a real folder answering you in real time from a new terminal. That folder, `my_agent`, is the basis for every lesson that follows.

```bash theme={null}
$ adk run my_agent
my_agent> Hi! How can I help you today?
```

Overview: what you'll do

* Create a clean Python virtual environment (venv).
* Install the ADK CLI package (`google-adk`).
* Get a Gemini API key from Google AI Studio.
* Keep that key out of source by using an environment variable.
* Scaffold a starter agent with `adk create my_agent`.
* Run and iterate on `my_agent`.

Create a virtual environment and install ADK
ADK is a Python package. Use a virtual environment to avoid dependency conflicts with other projects on your machine.

```bash theme={null}
$ python -m venv .venv
$ source .venv/bin/activate   # use `.venv\Scripts\activate` on Windows PowerShell/CMD
(.venv) $ pip install google-adk
Successfully installed google-adk-2.0.0
```

Verify the ADK CLI is available:

```bash theme={null}
(.venv) $ adk version
adk 2.0.0
```

Quick reference — common setup commands

| Step | Command / Example |
| - | - |
| Create & activate venv (macOS/Linux) | `python -m venv .venv` and `source .venv/bin/activate` |
| Activate venv (Windows PowerShell) | `.venv\Scripts\activate` |
| Install ADK package | `pip install google-adk` |
| Check ADK version | `adk version` |

Choose a model and get an API key
ADK is built to work with Gemini models. The fastest way to start is with a free API key from Google AI Studio. Sign in, click "Get API Key," and copy the key string.

* Recommended model: `gemini-flash-latest` — fast and cost-effective for most tasks.
* Backend: Google AI Studio — paste your API key when prompted by the ADK CLI.

Important: do not hard-code API keys in source files
Never paste your API key directly into source code. If you put the key into `agent.py` or any committed file, it can be accidentally published and abused.

```python theme={null}
# insecure example — do NOT commit API keys in source
agent = LlmAgent(
    model="gemini-flash-latest",
    api_key="AIzaSyD4q8e...Jk8Qw",
)
```

Store the key in an environment variable named `GOOGLE_API_KEY`. ADK reads that environment variable automatically when your agent starts.

```bash theme={null}
# macOS / Linux
$ export GOOGLE_API_KEY="AIzaSyD4q8e-7mXk2Lw9vR1nQ6tZpYb3Hc0Jk8Qw"

# Windows PowerShell
> $Env:GOOGLE_API_KEY = "AIzaSyD4q8e-7mXk2Lw9vR1nQ6tZpYb3Hc0Jk8Qw"
```

Never commit API keys or secrets to source control. Use environment variables or a secrets manager instead.

Tip: After exporting `GOOGLE_API_KEY` in your shell, ADK will automatically pick it up the next time you run `adk run` or any other ADK command in that shell session.

ADK is code-first
Your agent is plain Python you can open, read, and edit — not a config file or a drag-and-drop canvas. That means you can version, test, and iterate using standard development workflows.

![An infographic titled "Code First" showing two panels: one for Python with a code icon and buttons labeled "Open, Read, Edit," and another for "Config File or Canvas" with icons for config files and a canvas. Below them is a dark code editor window labeled "agent.py."](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/Fundamentals-of-Google-ADK/Setting-Up-Your-Agents/code-first-python-config-canvas-agentpy.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=5f8015fc4e5ec2538050ffde6bd64816)

Scaffold a starter agent with the ADK CLI
The ADK CLI can scaffold a starter agent for you. Run the create command and answer two prompts: which model and which backend.

* When prompted, choose `gemini-flash-latest` as the model.
* Choose Google AI Studio for the backend and paste your API key when asked.

Example interaction:

```bash theme={null}
$ adk create my_agent
? Which model do you need?
gemini-flash-latest
? Which backend do you prefer?
Google AI Studio
? Paste your API key: AIza••••••••••
Created my_agent/.env
Created my_agent/__init__.py
Created my_agent/agent.py
```

Files created by `adk create my_agent`

| File | Purpose |
| - | - |
| `my_agent/.env` | Environment configuration scaffold (do not commit secrets) |
| `my_agent/__init__.py` | Package initializer for the agent project |
| `my_agent/agent.py` | Scaffolded agent code — the file you'll edit and run |

Run your agent
Now you have a working project folder (`my_agent`) with a scaffolded `agent.py`. Start it and iterate from there; every lesson will add features to this same agent so you build on prior work rather than starting over.

```bash theme={null}
(.venv) $ adk run my_agent
my_agent> Hi! How can I help you today?
```

Links and references

* [Gemini models (Google)](https://developers.generativeai.google/models/gemini)
* [Google AI Studio — Get API Key](https://studio.google.ai)

Next steps

* Open `my_agent/agent.py` and run a simple query.
* Add or register tools deliberately, validate inputs, and add robust error handling to avoid crashes like the one above.
* Keep iterating: the agent is code-first, so make small, testable changes and use version control to avoid losing your work.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/google-adk-2-0/module/1d143913-38fb-4a53-8976-041e503dbf12/lesson/9f654afc-e4c7-4e96-b643-71331f1096a6)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.