AI Agents

Custom Tool Creation: @tool Decorator, StructuredTool, Schema Validation, and Tool Composition

Agents need tools. Build custom tools with the @tool decorator, define schemas for inputs, and compose tools into agent toolkits for specialized tasks.

SS
Soham Sharma
AI Engineer, Botmartz · July 17, 2026 · 1 min read
Read Time
1 min
Failure Modes
5
Code Snippets
3
Runnable Notebook
1
Custom Tool Creation: @tool Decorator, StructuredTool, Schema Validation, and Tool Composition

Tools are callable functions that agents invoke to interact with the world. The @tool decorator turns Python functions into agent tools. StructuredTool with Pydantic schemas provides type safety and self-documenting interfaces.

Simple Tools with @tool

from langchain.agents import tool

@tool def search_wikipedia(query: str) -> str: """Search Wikipedia for information about a topic.""" # Implementation return f"Results for {query}"

@tool def get_weather(city: str) -> str: """Get the current weather in a city.""" # Implementation return f"Weather in {city}"

# Agents can now call these tools

Structured Tools with Pydantic

from langchain.agents import StructuredTool

from pydantic import BaseModel, Field

class SearchInput(BaseModel): query: str = Field(description="Search query") limit: int = Field(default=5, description="Number of results")

def search_function(query: str, limit: int) -> str: # Implementation return f"Found {limit} results for {query}"

search_tool = StructuredTool.from_function( func=search_function, args_schema=SearchInput, )

Conclusion

Custom tools enable agents to interact with external systems. Structured tools with schemas ensure type safety and clear interfaces. Building a toolkit of domain-specific tools powers specialized agents. Next: tracing and debugging agent execution with LangSmith.

Closing Takeaways

Measure retrieval precision and recall in isolation before touching the model.
Chunk along document structure, not arbitrary character counts.
Combine vector and keyword search — hybrid retrieval beats either alone.
Treat evaluation as continuous infrastructure, not a launch-week report.
Try It Yourself
A runnable Google Colab notebook with the eval harness and hybrid search code from this post.
#Agents#Tools#API Integration#Pydantic
0 views
SS
Soham Sharma
AI Engineer at Botmartz, building enterprise RAG and agent systems in production. Contributing to open-source libraries.

Discussion (0)

No approved comments yet. Be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *

More Engineering Insights
TensorFlow>-
Soham Sharma · 8 min read
GeneralPlaywright E2E Test Post
Integration Bot · 5 min read