A Information to Constructing a Self-Documenting AI


Typically, we spend a big period of time making an attempt to know blocks of code, comprehend the parameters, and decipher different complicated facets of the code. I assumed to myself, can the much-hyped AI Brokers assist me on this regard? The system that I have been to create had a transparent goal: to supply useful feedback, flags on duplicate variables, and, extra importantly, check capabilities to see the pattern outputs myself. This seems to be very a lot potential, proper? Let’s design this Agentic system utilizing the favored LangGraph framework. 

LangGraph for Brokers

LangGraph, constructed on high of LangChain, is a framework that’s used to create and orchestrate AI Brokers utilizing stateful graphs (state is a shared knowledge construction used within the workflow). The graph consists of nodes, edges, and states. We’ll not delve into advanced workflows; we’ll create a easy workflow for this challenge. LangGraph helps a number of LLM suppliers like OpenAI, Gemini, Anthropic, and so on. Be aware that I’ll be sticking to Gemini on this information. Instruments are an essential asset for Brokers that assist them lengthen their capabilities. What’s an AI Agent, you ask? AI Brokers are LLM-powered, which may cause or assume to make selections and use instruments to finish the duty. Now let’s proceed to designing the circulation and coding the system. 

Workflow of the system

The aim of our system will probably be so as to add documentation strings for capabilities and flag any points as feedback within the code. Additionally, to make this technique smarter, we’ll verify if the feedback exist already to skip including the identical utilizing the agentic system. Now, let’s have a look at the workflow I’ll be utilizing and delve into it. 

Flowchart

So, as you’ll be able to see, we’ve got a analysis node which is able to use an agent that can have a look at the enter code and in addition “cause” if there may be documentation already current. It’ll then use conditional routing utilizing the ‘state’ to resolve the place to go subsequent. First, the documentation step writes the code based mostly on the context that the analysis node gives. Then, the evaluation node exams the code on a number of check instances utilizing the shared context. Lastly, the final node saves the knowledge in evaluation.txt and shops the documented code in code.py.

Coding the Agentic system

Pre-requisites

We’ll want the Gemini API Key to entry the Gemini fashions to energy the agentic system, and in addition the Tavily API Key for net search. Be sure you get your keys from the hyperlinks beneath:

Gemini: https://aistudio.google.com/apikey
Tavily: https://app.tavily.com/dwelling

For simpler use, I’ve added the repository to GitHub, which you’ll be able to clone and use:

https://github.com/bv-mounish-reddy/Self-Documenting-Agentic-System.git

Make sure that to create a .env file and add your API keys: 

GOOGLE_API_KEY=

TAVILY_API_KEY=

I used the gemini-2.5-flash all through the system (which is free to an extent) and used a few instruments to construct the system. 

Device Definitions

In LangGraph, we use the @instrument decorator to specify that the code/perform will probably be used as a instrument. We now have outlined these instruments within the code:

# Instruments Definition

@instrument

def search_library_info(library_name: str) -> str:

   """Seek for library documentation and utilization examples"""

   search_tool = TavilySearchResults(max_results=2)

   question = f"{library_name} python library documentation examples"

   outcomes = search_tool.invoke(question)

   formatted_results = []

   for end in outcomes:

       content material = end result.get('content material', 'No content material')[:200]

       formatted_results.append(f"Supply: {end result.get('url', 'N/A')}nContent: {content material}...")

   return "n---n".be a part of(formatted_results)

This instrument is utilized by the analysis agent to know the syntax related to the Python libraries used within the enter code and see examples of the way it’s getting used. 

@instrument

def execute_code(code: str) -> str:

   """Execute Python code and return outcomes"""

   python_tool = PythonREPLTool()

   strive:

       end result = python_tool.invoke(code)

       return f"Execution profitable:n{end result}"

   besides Exception as e:

       return f"Execution failed:n{str(e)}"

This instrument executes the code with the inputs outlined by the evaluation agent to confirm whether or not the code works as anticipated and to verify for any loopholes.

Be aware: These capabilities are outlined utilizing the inbuilt LangGraph instruments: PythonREPLTool() and TavilySearchResults().

State Definition

The shared knowledge within the system must have a transparent construction to create a great workflow. I’m creating the construction as a TypedDict with the variables I’ll be utilizing within the agentic system. The variables will present context to the next nodes and in addition assist with the routing within the agentic system:

# Simplified State Definition

class CodeState(TypedDict):

   """Simplified state for the workflow"""

   original_code: str

   documented_code: str

   has_documentation: bool

   libraries_used: Listing[str]

   research_analysis: str

   test_results: Listing[str]

   issues_found: Listing[str]

   current_step: str

Agent Definitions

We used a ReAct (reasoning and performing) fashion agent for the ‘Analysis Agent’, which must be taught and cause. A ReAct-style agent can merely be outlined utilizing create_react_agent perform by passing the parameters, and this agent will probably be used within the node. Discover that we’re passing the beforehand outlined instrument within the create_react_agent perform. The node utilizing this Agent additionally updates a few of the state variables, which will probably be handed as context.

# Initialize Mannequin

def create_model():

   """Create the language mannequin"""

   return ChatGoogleGenerativeAI(

       mannequin="gemini-2.5-flash",

       temperature=0.3,

       google_api_key=os.environ["GOOGLE_API_KEY"]

   )

# Workflow Nodes

def research_node(state: CodeState) -> CodeState:

   """

   Analysis node: Perceive code and verify documentation

   Makes use of agent with search instrument for library analysis

   """

   print("RESEARCH: Analyzing code construction and documentation...")

   mannequin = create_model()

   research_agent = create_react_agent(

       mannequin=mannequin,

       instruments=[search_library_info],

       immediate=ChatPromptTemplate.from_messages([

           ("system", PROMPTS["research_prompt"]),

           ("placeholder", "{messages}")

       ])

   )

   # Analyze the code

   analysis_input = {

       "messages": [HumanMessage(content=f"Analyze this Python code:nn{state['original_code']}")]

   }

   end result = research_agent.invoke(analysis_input)

   research_analysis = end result["messages"][-1].content material

   # Extract libraries utilizing AST

   libraries = []

   strive:

       tree = ast.parse(state['original_code'])

       for node in ast.stroll(tree):

           if isinstance(node, ast.Import):

               for alias in node.names:

                   libraries.append(alias.identify)

           elif isinstance(node, ast.ImportFrom):

               module = node.module or ""

               for alias in node.names:

                   libraries.append(f"{module}.{alias.identify}")

   besides:

       cross

   # Test if code has documentation

   has_docs = ('"""' in state['original_code'] or

               "'''" in state['original_code'] or

               '#' in state['original_code'])

   print(f"  - Libraries discovered: {libraries}")

   print(f"  - Documentation current: {has_docs}")

   return {

       **state,

       "libraries_used": libraries,

       "has_documentation": has_docs,

       "research_analysis": research_analysis,

       "current_step": "researched"

   }

Equally, we outline the opposite brokers as effectively for the nodes and tweak the prompts as wanted. We then proceed to outline the sides and workflow as effectively. Additionally, discover that has_documents variable is crucial for the conditional routing within the workflow.

Outputs

You possibly can change the code in the primary perform and check the outcomes for your self. Right here’s a pattern of the identical:

Enter code

sample_code = """

import math

import random

def calculate_area(form, **kwargs):

   if form == "circle":

       return math.pi * kwargs["radius"] ** 2

   elif form == "rectangle":

       return kwargs["width"] * kwargs["height"]

   else:

       return 0

def divide_numbers(a, b):

   return a / b

def process_list(objects):

   whole = 0

   for i in vary(len(objects)):

       whole += objects[i] * 2

   return whole

class Calculator:

   def __init__(self):

       self.historical past = []

   def add(self, a, b):

       end result = a + b

       self.historical past.append(f"{a} + {b} = {end result}")

       return end result

   def divide(self, a, b):

       return divide_numbers(a, b)

calc = Calculator()

end result = calc.add(5, 3)

space = calculate_area("circle", radius=5)

division = calc.divide(10, 2)

objects = [1, 2, 3, 4]

processed = process_list(objects)

print(f"Outcomes: {end result}, {space:.2f}, {division}, {processed}")

"""

Pattern Output

Documented Code
Example usage

Discover how the system says the random module is imported however not used. The system provides docstrings, flags points, and in addition provides feedback within the code about how the capabilities are getting used. 

Conclusion

We constructed a easy agentic system with the usage of LangGraph and understood the significance of state, instruments, and brokers. The above system will be improved with the usage of further nodes, instruments, and refinements within the prompts. This technique will be prolonged to constructing a debugging system or a repository builder as effectively, with the appropriate nodes and instruments. Additionally, do not forget that utilizing a number of brokers can even end in larger prices when utilizing a paid mannequin, so create and use brokers that add worth to your agentic programs and outline the workflows effectively prematurely. 

Continuously Requested Questions

Q1. What’s LangGraph’s @instrument decorator?

A. It’s the way you mark a perform so a LangGraph agent can name it as a instrument inside workflows.

Q2. What’s ReAct in brokers?

A. It’s the loop the place brokers cause step-by-step, act with instruments, then observe outcomes.

Q3. Can this lengthen to real-world apps?

A. Yeah, you’ll be able to plug it into audits, debugging, compliance, and even stay data bases. Code documentation is likely one of the use instances.

Captivated with know-how and innovation, a graduate of Vellore Institute of Expertise. Presently working as a Information Science Trainee, specializing in Information Science. Deeply thinking about Deep Studying and Generative AI, desperate to discover cutting-edge strategies to unravel advanced issues and create impactful options.

Login to proceed studying and revel in expert-curated content material.