Step 2: Write the Agent Code Create a file named and add the following code. This script sets up a calculator tool and hands it to the Gemini model. import os from google import genai from google.genai import types # 1. Define a tool (a standard Python function) def add_numbers(a: float, b: float) -> float: """Adds two numbers together and returns the result.""" return a + b # 2. Initialize the client (Make sure GEMINI_API_KEY is set in your environment variables) client = genai.Client() # 3. Define the agent's personality and tools system_instruction = "You are a helpful assistant. Use your tools whenever a math question is asked." my_tools = [add_numbers] # 4. Start a chat session with the agent chat = client.chats.create( model="gemini-2.5-flash", config=types.GenerateContentConfig( system_instruction=system_instruction, tools=my_tools, temperature=0.0 # Low temperature keeps the agent logical and stable ) ) # 5. Test the agent loop user_message = "Hey! Can you add 143.5 and 256.2 for me?" print(f"User: {user_message}") # The SDK automatically handles the "Observe-Think-Act" loop under the hood! # It sends the prompt, sees that the model wants to use 'add_numbers', # executes the Python function local to your machine, sends the result back to Gemini, # and returns the final human-readable answer. response = chat.send_message(user_message) print(f"Agent: {response.text}") #buildai #sidehustle #python - @tadeniyi45"/> Step 2: Write the Agent Code Create a file named and add the following code. This script sets up a calculator tool and hands it to the Gemini model. import os from google import genai from google.genai import types # 1. Define a tool (a standard Python function) def add_numbers(a: float, b: float) -> float: """Adds two numbers together and returns the result.""" return a + b # 2. Initialize the client (Make sure GEMINI_API_KEY is set in your environment variables) client = genai.Client() # 3. Define the agent's personality and tools system_instruction = "You are a helpful assistant. Use your tools whenever a math question is asked." my_tools = [add_numbers] # 4. Start a chat session with the agent chat = client.chats.create( model="gemini-2.5-flash", config=types.GenerateContentConfig( system_instruction=system_instruction, tools=my_tools, temperature=0.0 # Low temperature keeps the agent logical and stable ) ) # 5. Test the agent loop user_message = "Hey! Can you add 143.5 and 256.2 for me?" print(f"User: {user_message}") # The SDK automatically handles the "Observe-Think-Act" loop under the hood! # It sends the prompt, sees that the model wants to use 'add_numbers', # executes the Python function local to your machine, sends the result back to Gemini, # and returns the final human-readable answer. response = chat.send_message(user_message) print(f"Agent: {response.text}") #buildai #sidehustle #python - @tadeniyi45 - Tikwm"/> Step 2: Write the Agent Code Create a file named and add the following code. This script sets up a calculator tool and hands it to the Gemini model. import os from google import genai from google.genai import types # 1. Define a tool (a standard Python function) def add_numbers(a: float, b: float) -> float: """Adds two numbers together and returns the result.""" return a + b # 2. Initialize the client (Make sure GEMINI_API_KEY is set in your environment variables) client = genai.Client() # 3. Define the agent's personality and tools system_instruction = "You are a helpful assistant. Use your tools whenever a math question is asked." my_tools = [add_numbers] # 4. Start a chat session with the agent chat = client.chats.create( model="gemini-2.5-flash", config=types.GenerateContentConfig( system_instruction=system_instruction, tools=my_tools, temperature=0.0 # Low temperature keeps the agent logical and stable ) ) # 5. Test the agent loop user_message = "Hey! Can you add 143.5 and 256.2 for me?" print(f"User: {user_message}") # The SDK automatically handles the "Observe-Think-Act" loop under the hood! # It sends the prompt, sees that the model wants to use 'add_numbers', # executes the Python function local to your machine, sends the result back to Gemini, # and returns the final human-readable answer. response = chat.send_message(user_message) print(f"Agent: {response.text}") #buildai #sidehustle #python - @tadeniyi45"/>

@tadeniyi45: How To Build AI Agent From Scratch. To build an AI agent from scratch, create a core Observe-Think-Act loop. Connect a large language model as the "brain" for reasoning, provide a system prompt to define its role and constraints, attach short-term/long-term memory, and code external functions or APIs as "tools" for execution. Core Components of an AI Agent The Brain (LLM): Handles intent parsing, multi-step planning, and decision-making. The Brain Stem (System Prompt): Sets the persona, boundaries, and rules for how the agent behaves and when to use specific tools. Memory: Maintains short-term conversational context and long-term data storage or preference logs. Tools: Grants functional capability via custom scripts, APIs, or protocols like Model Context Protocol (MCP) to interact with databases, web search, or email. Step-by-Step Implementation Guide Map the Process: Write down the exact steps of the workflow you want to automate, prioritizing a low-precision, time-intensive task where 90% accuracy is acceptable. Build the Deterministic Loop: Set up a basic programmatic loop (such as a while loop in Python or Node.js) to continuously read user or system input, process it, and wait for the next command. Integrate the Model: Connect your loop to a hosted LLM API (like OpenAI or Google Gemini) or a local model runtime (like Ollama). Define and Bind Tools: Write standard functions (e.g., a calculator, a weather fetcher, or a database query) and format them so the LLM can output a structured tool call when it needs external data. Add Guardrails and Iteration: Implement validation steps, error handling for failed tool executions, and a human-in-the-loop escalation path for critical errors. If you prefer building using pre-existing architectures rather than raw code, many developers recommend frameworks and visual orchestration tools mentioned by the community on platforms like Reddit: ◇ For Python Developers: CrewAI, LangGraph, or LlamaIndex. ◇ For Low-Code/Visual Builders: n8n or Flowise. TO USE PYTHON To build an AI agent from scratch using pure Python, you write code that connects an LLM to a loop. The LLM decides what to do, and your Python code executes those actions. Here is a complete, minimal guide to building a functional agent using the official Google GenAI SDK. Step 1: Install the Required Library First, open your terminal and install the official Google GenAI package. bash Step 2: Write the Agent Code Create a file named and add the following code. This script sets up a calculator tool and hands it to the Gemini model. import os from google import genai from google.genai import types # 1. Define a tool (a standard Python function) def add_numbers(a: float, b: float) -> float: """Adds two numbers together and returns the result.""" return a + b # 2. Initialize the client (Make sure GEMINI_API_KEY is set in your environment variables) client = genai.Client() # 3. Define the agent's personality and tools system_instruction = "You are a helpful assistant. Use your tools whenever a math question is asked." my_tools = [add_numbers] # 4. Start a chat session with the agent chat = client.chats.create( model="gemini-2.5-flash", config=types.GenerateContentConfig( system_instruction=system_instruction, tools=my_tools, temperature=0.0 # Low temperature keeps the agent logical and stable ) ) # 5. Test the agent loop user_message = "Hey! Can you add 143.5 and 256.2 for me?" print(f"User: {user_message}") # The SDK automatically handles the "Observe-Think-Act" loop under the hood! # It sends the prompt, sees that the model wants to use 'add_numbers', # executes the Python function local to your machine, sends the result back to Gemini, # and returns the final human-readable answer. response = chat.send_message(user_message) print(f"Agent: {response.text}") #buildai #sidehustle #python

Tayo Adeniyi
Tayo Adeniyi
Open In TikTok:
Region: NG
Wednesday 12 August 2026 10:31:48 GMT
12986
135
0
3

Music

Download

Comments

There are no more comments for this video.
To see more videos from user @tadeniyi45, please go to the Tikwm homepage.

About


© 2021-2026 TikWM. All rights reserved.